Variable must be in a Module??

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

I have a project that contains a usercontrol, some forms and a module.

The only thing in the module is one variable that is there so that it can be
used by the control and all the forms.

Couldn't I put the variable in the usercontrol and somehow set it so that it
can be used by usercontrol and all the forms?

If so, how?

Thanks
 
Just Me said:
I have a project that contains a usercontrol, some forms and a module.

The only thing in the module is one variable that is there so that it can
be used by the control and all the forms.

Couldn't I put the variable in the usercontrol and somehow set it so that
it can be used by usercontrol and all the forms?

You can add a property to the form or usercontrol and then pass around a
reference to the {form, usercontrol}'s instance in order to be able to
access the property.
 
I do use that trick (thanks to previous help from you) but I'm now wondering
what is special about a variable declared in a module. Whatever is special
about a variable in a module by default, can't I explicitly create such a
variable in a class.

Thanks
 
A module is just a VB syntax convenience for a class with only static (read
Shared members).

So

Module Test
Public A As String
Public B As String
End Module

is the same as

Class Test
Public Shared A As String
Public Shared B As String
End Class

In either case, you can refer to Test.A. So you can do the same thing in a
class as a module. You'll find when you look around the Framework, that this
feature is used in lots of places. Just for a start, the
System.Windows.Forms.MessageBox class has a Shared Show method.

HTH

Nigel Armstrong

Just Me said:
I do use that trick (thanks to previous help from you) but I'm now wondering
what is special about a variable declared in a module. Whatever is special
about a variable in a module by default, can't I explicitly create such a
variable in a class.

Thanks
 
I was hoping that was true.

At first I thought I was going to say:
The example is a little different because there you use the class name and
the method.
Here we want to use only the variable name.

But I'm guessing that within a module you don't need the class name for
static variables. Is that the way to say it or is there a more general rule?

Thanks

Nigel Armstrong said:
A module is just a VB syntax convenience for a class with only static (read
Shared members).

So

Module Test
Public A As String
Public B As String
End Module

is the same as

Class Test
Public Shared A As String
Public Shared B As String
End Class

In either case, you can refer to Test.A. So you can do the same thing in a
class as a module. You'll find when you look around the Framework, that
this
feature is used in lots of places. Just for a start, the
System.Windows.Forms.MessageBox class has a Shared Show method.

HTH

Nigel Armstrong
 
Back
Top