Reputation: 309
I have the following code in my Form1 wherein I will set Value to a property:
Dim login As New logInSession
Dim k As String
login.saveLogInfo = unameTB.Text
and this is my code from my logInSession class where I will store the value:
Private logInfo As String
Public Property saveLogInfo() As String
Get
Return logInfo
End Get
Set(ByVal value As String)
logInfo = value
End Set
End Property
Now, I want to get back out the value to my Form2. The problem is it doesn't return any value. The following is my code:
Dim login As New logInSession
Dim k As String
k = login.saveLogInfo
MsgBox(k)
Can you tell me what's the problem with my codes?
Upvotes: 1
Views: 95
Reputation: 89285
That's because you have two different instances of logInSession
class. One in Form1
and another in Form2
. Here is an illustration :
Dim login As New logInSession
'you have 1 logInSession instance here, and set it's property
login.saveLogInfo = "Some Text"
Dim anotherLogin As New logInSession
'but later you check property of another instance of logInSession
Dim k = anotherLogin.saveLogInfo
'here you get empty string
Console.WriteLine(k)
'you need to, in some way, pass the first instance instead of creating new instance
Dim referenceToFirstInstance As logInSession = login
k = anotherLogin.saveLogInfo
'here you get "Some Text"
Console.WriteLine(k)
Check this reference on how to pass data between forms
Upvotes: 1
Reputation: 20794
Dim login As New logInSession // HERE
Dim k As String
k = login.saveLogInfo
MsgBox(k)
This code creates a new instance of the logInSession
class. Therefore, each time you are trying to access the login.saveLogInfo
property, it is at it's default value (empty string). You need to use the original object you created here:
Dim login As New logInSession
Dim k As String
login.saveLogInfo = unameTB.Text
Upvotes: 1