-
May 17th, 2019, 08:04 AM
#1
Thread Starter
PowerPoster
Why is my variable not recognised ?
Hi,
I have a boolean variable called 'boss' declared in Form1 as Public.
I set it to True in Form1, but when I come to use it in Form2 it's not recognised.
Setting a breakpoint at the relevant code, and entering boss in Watch1 window, at the Form1 point where boss is set to True I can step through and watch boss change from False to True.
When I reach the breakpoint in Form2, the Watch1 window still says boss is true, but it's sort of greyed out. So, in the same window I enter 'Form1.boss'
I get the error:
Form1.boss error BC30469: Reference to a non-shared member requires an object reference.
Stepping past the ' If Form1.boss Then' line execution jumps to 'Else'.
Code:
'__________________ Top of Form1 ___________________
Public Class Form1
Public sh As Int32 = My.Computer.Screen.Bounds.Size.Height 'This screen height
Public sx As Int32 = My.Computer.Screen.Bounds.Size.Width
Public chk As Int32
Public boss As Boolean
'__________________ Subroutine in Form2 ___________________
Public Sub Bossy()
If Form1.boss Then
Button1.Text = "150 Plots"
Button2.Text = "200 Plots"
Button3.Text = "350 Plots"
Button4.Text = "500 Plots"
Button5.Text = "700 Plots"
Else
Button1.Text = "Easy"
Button2.Text = "Mild"
Button3.Text = "Medium"
Button4.Text = "Hard"
Button5.Text = "Tough"
End If
End Sub
I don't understand why this is happening.
Poppa.
Microsoft Visual Studio Community 2017. Version 15.9.12
Microsoft .NET Framework. Version 4.7.03190
Windows 10 Home
Along with the sunshine there has to be a little rain sometime.
-
May 17th, 2019, 08:51 AM
#2
Re: Why is my variable not recognised ?
Pass the refernce to Boss to Form2
Code:
Form1:
Public Class Form1
Dim Boss As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using Form2 As New Form2(Boss)
Form2.ShowDialog()
End Using
End Sub
End Class
Code:
Form2:
Public Class Form2
Dim BossFromForm1 As Boolean
Public Sub New(ByVal Boss As Boolean)
InitializeComponent()
BossFromForm1 = Boss
End Sub
Public Sub Bossy()
If BossFromForm1 Then
Button1.Text = "150 Plots"
Button2.Text = "200 Plots"
Button3.Text = "350 Plots"
Button4.Text = "500 Plots"
Button5.Text = "700 Plots"
Else
Button1.Text = "Easy"
Button2.Text = "Mild"
Button3.Text = "Medium"
Button4.Text = "Hard"
Button5.Text = "Tough"
End If
End Sub
End Class
Last edited by kpmc; May 17th, 2019 at 10:02 AM.
-
May 17th, 2019, 09:19 AM
#3
Re: Why is my variable not recognised ?
Some might disagree on the programming style of it but you could also make it public in a module and it will be available throughout the entire project.
Please remember next time...elections matter!
-
May 17th, 2019, 09:24 AM
#4
Thread Starter
PowerPoster
Re: Why is my variable not recognised ?
Originally Posted by TysonLPrice
Some might disagree on the programming style of it but you could also make it public in a module and it will be available throughout the entire project.
I thought declaring a variable 'Public' ensures that it is global.
That is certainly the case everywhere else that I've used it.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 17th, 2019, 09:44 AM
#5
Re: Why is my variable not recognised ?
Originally Posted by Poppa Mintin
I thought declaring a variable 'Public' ensures that it is global.
That is certainly the case everywhere else that I've used it.
Poppa.
Nope. Declaring a member Public just means that it can be accessed outside its parent type. A global variable is, by definition, one that is accessible anywhere in your project. A Public instance field in a class isn't accessible everywhere because the object it's a member of isn't inherently accessible everywhere. To be global, the field would have to be declared Public as well as being declared in a module or else declared Shared in a class. That way you don't have to instantiate a class in order to be able to access the field.
Last edited by jmcilhinney; May 17th, 2019 at 09:47 AM.
-
May 17th, 2019, 09:55 AM
#6
Re: Why is my variable not recognised ?
The code you have in Form2 is attempting to use the default instance of Form1. The default instance of a form is accessed by using the class name as though it was a variable. If you are unable to access instance members that way then that suggests that there is no default instance. That happens if a form class has no parameterless constructor, because the system can only create an instance if doing so doesn't require any arguments. Have you declared a constructor in Form1 that has parameters?
Note that a Public instance field in a form class is not magically a global variable because the form has a default instance. Default instances are still instances and can thus be created and destroyed, plus they are thread-specific. All that means that accessing that field will not necessarily be the same field every time. A global variable is, by definition, a single variable that is accessible everywhere, all the time.
-
May 17th, 2019, 10:04 AM
#7
Thread Starter
PowerPoster
Re: Why is my variable not recognised ?
Oh !
Of course... Simple fix:
Code:
Public Shared boss As Boolean
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 17th, 2019, 10:09 AM
#8
Re: Why is my variable not recognised ?
Nice explanation jmc. Passing the variable as parameter will simply ensure that it's passing the current instance of the variable and can remain private to the source Class, which is how I understand it.
-
May 17th, 2019, 10:43 AM
#9
Re: Why is my variable not recognised ?
Yes there's a simple fix, but that is very bad programming practice. Much better to pass the value to form2 in a constructor as kpmc showed you...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|