PDA

View Full Version : public var not defined in other form


Ludootje
02-01-2004, 10:23 AM
I've got this code in frmSettings:
Private Sub Form_Load()
txt_settings_stats.Text = var_nr_stats
txt_settings_rnd1.Text = var_rnd_x
txt_settings_rnd2.Text = var_rnd_y
End Sub

When I want to open the settings form, VB6 tells me this:
Compile error:
Variable not defined

After I hit 'Ok' it leads me to the code it has a problem with, and in this case it highlights the "Private Sub Form_Load()" in yellow, puts a => before it; and also selects var_nr_stats

The variable is defined like this in frmMain's General Declarations:
Option Explicit
Public var_rnd_x As Integer
Public var_rnd_y As Integer
Public var_nr_stats As Integer

Then the variables are set here:
Private Sub Form_Load()
var_rnd_x = 1
var_rnd_y = 10
var_nr_stats = 5

frmMain.lbl_values.Caption = "The number is between " & var_rnd_x & ", " & var_rnd_y & "."
Call start_newgame
End Sub


The variables are public, so why doesn't this work?

(BTW 'defined' does mean that there's no such variable right, not that they have no value?)

Thanks!

Ludootje
02-04-2004, 11:26 AM
Using frmMain.var_nr_stats instead of just var_nr_stats fixed it.
What I don't understand is, what's the use of 'public' good for then?

DNAunion2000
02-09-2004, 05:09 PM
1) Without the variables being Public they wouldn't be accessible from outside the form they were declared in. So Public is required in order for your code contained in a second form to access them directly.

2) Just because an object's data members are Public and accessible doesn't mean that their names don't have to be qualified.

Ludootje
02-10-2004, 01:13 PM
Ok thank you.
I thought a 'Private fooBar' could also be accessed from another form using "frmMain.fooBar"

Thanks!

manual_overide
03-15-2004, 11:37 PM
Directly accessing data like that is dangerous. You could inadvertently get the wrong value for your variables or other such badness

You should use public properties and private data variables.


Private miVarRndX As Integer
.
.
.
Public Property Get RandomX() As Integer
RandomX = miVarRndX
End Property

' optional if you want to set the number the same way
Public Propery Let RandomX(iData as Integer)
miVarRndX = iData
End Property


and in your Main form


Dim iRndX As Integer
.
.
.
Load frmFoo
With frmFoo
.Show
iRndX = .RandomX
End With
Unload frmFoo

Ludootje
03-16-2004, 01:24 PM
I don't know public properties & private data variables, so I can't really use them :o (still a newbie heh)

However, I'm interested in knowing what exactly you mean by "Directly accessing data like that is dangerous."
What's dangerous about this? How can it be abused?

Thanks!

little birdy
03-16-2004, 10:44 PM
typically, you don't want your data to be directly accessed by outside objects. it's the notion of encapsulating data within objects. so, for other things to get at them, you use properties or accessor methods that return the value. basically, if you code a property to return a variable, instead of just directly accessing the variable, you are more likely to get the right thing. because the variable name could change, or it's value might not be set, or something else. it's a combination of making things easier (with a little extra code) and protecting your data from outside interference.