Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing data between classes

Status
Not open for further replies.

deejayAr

Technical User
Mar 20, 2008
126
0
0
US

I have property in a class
Public Class ClsAIcomp
Private _pContext As String
Public Property pContext() As String
Get
Return _pContext
End Get

Set(ByVal Value As String)
_pContext = Value
End Set
End Property
end Class
and I'm setting value of pContext in the same class

Now I need to read this value in other class say secondclass
I'm using like this

Dim myaicomp As new ClsAIcomp
dim strabc as string = myaicomp.pContext

but I see nothing in the value
what I'm doing wrong
and what is the best way to pass the value between the classes


any help would be appriciated



 
Well, in this line
Code:
Dim myaicomp As  new ClsAIcomp
you have created a new instace of your ClsAIcomp class. If you had another instance of that class declared somewhere else, this new instance won't pick up the other instances values. So in this case, your pContext is reinitialized.

Properties are a great way to pass values. But for your particular problem, what are you doing exactly? Have you already created an instance of ClsAIcomp somewhere else in your application, and you're trying to get the value out of it in a different section of your prorgram?
 
yeah, there is an other class using a instance of ClsAIcomp to store value into pContext property
and now I need to get the value in second class let say
clsLink
If I do not use Dim myaicomp As new ClsAIcomp
then program generate run time error.

 
I have class name ClsAIcomp.vb
this class contains all properties
code
Public Class ClsAIcomp


Private _pContext As String
Public Property pContext() As String
Get
Return _pContext
End Get

Set(ByVal Value As String)
_pContext = Value
End Set
End Property

' Pluse other properties
End Class

Now I have class name clsCadlink doing some calculation and save the value into pContext property of ClsAIcomp class
code for clsCadLink
Imports System
Imports System.Diagnostics
Imports System.Threading
Imports System.Configuration
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Text
Imports System.Xml.XPath
Imports System.IO


Public Class clsCadlink

Dim pClsAIcomp As New ClsAIcomp
' If I use Dim pClsAIcomp As ClsAIcomp
' it will run time error and still can not pass the value

Private Sub AICompXML(ByVal strxml As String)
Dim rawData As String = strxml
Dim xmlDoc As New XmlDocument
Dim LstNodes As XmlNodeList
Dim XNodes As XmlNode
Dim baseDataNodes As XmlNodeList
xmlDoc.LoadXml(rawData)
' some process
' then I save value into pContext property of class ClsAIcomp to use other classess

pClsAIcomp.pContext = xmlDoc.SelectSingleNode("//CONTEXT").InnerXml
End Sub

End Class
and then other part of application I have a class name

and then other part of application another class name CCProxy1Impl is using pContext property value from pClsAIcomp class
code for CCProxy1Impl class
Imports System.Diagnostics
Imports System.Threading
Imports System.Configuration

Imports System.IO

Public Class CCProxy1Impl
Public Sub New()
MyBase.New()
End Sub



Protected Sub Z_Cad_Link(ByRef Xml As String)
' I'm doing lot of work here and then I need to check the value of pContext

Dim myaicomp As New ClsAIcomp
' if I declare as Dim myaicomp As ClsAIcomp
' it will create run time error and still empty
If myaicomp.pContext = "F" Then
' call function name Z_Cad_Call(Xml)
Else
' call function name Z_Cad_SELL(Xml)
End If
End Sub

End Class

Hope I explain my problem very well
thank you very much for helping me...


 
You've declared an instance here
Code:
Dim pClsAIcomp As New ClsAIcomp

Its accessible throughout your class clsCadlink, but nowhere else.

You then want to access that value from your CCProxy1Impl class.

However, in clsCadlink, you are not creating an instance of CCProxy1Impl, so you cannot pass values from one to the other.

To resolve this, you should declare your pClsAIcomp to be available to both classes. Declare it inside of a module instead of your class, and it will be accessible throughout your project.
 
Thank you very much your response
it's now working
great
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top