Assigning values to variables from another form

ljpv14

Active member
Joined
Nov 28, 2011
Messages
44
Programming Experience
3-5
HELP! I don't know what happened but I've been spending sometime thinking of what could have gone wrong with my code. I'm passing a value to another form and later on use that value that is assigned to a variable.

Here is the code.
loadingPage.Show()
mode = "notime"
Dim ld As loadingPage
ld = New loadingPage()
ld.setMode(mode)


The loading page has this public sub method

Public Sub setMode(ByVal mode As String)
currentMode = mode
End Sub

After loading stuffs and it's animation, the loading page class will be calling my gameplay class

Me.Hide()
Dim gp As GamePlay
gp = New GamePlay()
gp.setMode(currentMode)
GamePlay.Show()



Then in my gameplay class I also have this public sub methos

Public Sub setMode(ByVal mode As String)
currentMode = mode
Console.WriteLine(mode + " : " + currentMode)
End Sub

Then I will be using my currentMode variable for if statements. but it seems that the currentMode does not have any value. When I try executing the console.WriteLine command at my setMode method, gameplay class there will be a values for the passing variable and the receiving variable. but when I use it at my code:

Private Sub displayInfinityIcon()
Console.WriteLine("mode = " + currentMode)
If currentMode = "notime" Then
infinityIcon.Show()
ElseIf currentMode = "withtime" Then
infinityIcon.Hide()
End If
End Sub

CurrentMode doesn't have a value. I don't know why. CurrentMode is a global variable. Help please!
 
The problem is that you are using two different forms. You display one 'loadingPage' instance and then you create another one and set its mode. Follow the Blog link in my signature and read the post on Default Instances. You should then be able to see what you're doing wrong in your first code snippet.
 
I tried creating only one instance of my GamePlay class. When I tried debugging my code, I can see that my setMode method is being called twice, the first call have values then the second call don't have any values. Any idea?
 
I didn;t say anything about your GamePlay class. Did you read my post? I specifically said:
You display one 'loadingPage' instance and then you create another one and set its mode.
Did you do as I suggested and read my blog post on default instances? If not, why not? If you did, do you understand what a default instance is and how it works and how your using one in your code is part of the issue?
 
I really find it hard to understand the blog. I just made something up and use a notepad to the values I need and it works. Thanks for the help anyways. :D

cheers!
 
If someone directs you to some information and you don't understand it, it's a good idea to say that you don't understand it, rather than act like it never happened. If I say "read this" and you post back without any indication that you read it then I assume that you haven't. If you say that you did but didn't understand, then I can help you understand by providing further explanation.

In this code:
loadingPage.Show()
mode = "notime"
Dim ld As loadingPage
ld = New loadingPage()
ld.setMode(mode)
you are using two different instances of the 'loadingPage' class. You display one and then update another. That's pretty easy to understand. If I was to show you the first page of a note book and then I took another note book and wrote on the first page, would you expect that writing to appear on the page you were looking at? Of course not. They are both note books but they are two different note books, so what happens to one doesn't affect the other. The same goes in your code. You display the default instance of the 'loadingPage' class by calling Show on the class name itself. You then proceed to create a new instance of the 'loadingPage' class, i.e. a second object of the same type, and call its 'setMode' method. Why would you expect calling 'setMode' on the second instance to have an effect on the first, any more than writing in one notebook should affect the other? You need to use just one instance, so either use the default instance all the time:
loadingPage.Show()
mode = "notime"
loadingPage.setMode(mode)
or else create your own instance and use it all the time:
Dim ld As loadingPage
ld = New loadingPage()
ld.Show()
mode = "notime"
ld.setMode(mode)
On an unrelated note, would it not make sense to call 'setMode' before calling Show?
 
I feel bad about myself for doing that. I really owe you one. I've been coding for 3 years but I still really need to learn more. Sorry for not being specific. Anyways, thank you for your kindness and thoughtfulness. I've been learning a lot from you. :D
 
Back
Top