Public Variables

  • Thread starter Thread starter ElenaR
  • Start date Start date
E

ElenaR

I have three variables which I have made Public. The
other forms in my project are still not able to read these
variables. Am I missing something?
 
Hi Elena,

I think we'll need some more information - how you are declaring the
variables, how you are accessing them, and what error is reported.

Regards,
Fergus
 
this might help..

In Form1's code:

Public VariableName as Type

To call from another form or module:

Form1.VariableName
 
Hi Jimmy,

|| Public VariableName as Type
|| Form1.VariableName

That's fine in VB6 where Form1 is the actual Form. In VB.NET, Form1 is the
Type <only>.

If you had
Class Form1
Public Shared VariableName As SomeType
then
Form1.VariableName would access it correctly - it wouldn't however
belong to the <instance> of Form1 but to the Form1 Class - and hence all
instances of Form1.

This wouldn't, of course, be a problem if you will only have one - in fact
you could have all your variables Shared!

To access a Form's variables from another class (Form or otherwsie), you
need a reference to the actual instance. This can be done in several ways.

One is to store the reference to Form1 in its own Class as a Shared
variable. As noted above, this only works if there will be <only one>
instance.

Class Form1

Public Shared TheActualForm1 As Form1
Public Sub New
TheActualForm1 = Me

Class Foo
Sub Dibbly
Dim T As String = TheActualForm1.Text

Another method is to create a Module and store it there. (Note that Shared
is not needed as it's done for you behind the scenes)

Public Module GlobalStuff
Public TheActualForm1 As Form1

Class Form1
Public Sub New
TheActualForm1 = Me

Other methods involve passing a reference (ie. Me) from Form1 to the other
class.

Regards,
Fergus
 
* "ElenaR said:
I have three variables which I have made Public. The
other forms in my project are still not able to read these
variables. Am I missing something?

Where did you declare these variables?
 
Back
Top