How can I have global variables?

M

MeAgin

Hi all,



I want to maintain few variables available for all the classes within the
system. For example the session key and the logged in user'd ID. How can I
do this? I can have a class with all these as properties. But how can I make
a instance of that classes available everywhere?

Thanks.

Nadee
 
A

Armin Zingler

MeAgin said:
I want to maintain few variables available for all the classes
within the system. For example the session key and the logged in
user'd ID. How can I do this? I can have a class with all these as
properties. But how can I make a instance of that classes available
everywhere?


For example by making it available by a Public Shared Readonly Property. I
usually have an Application class for this purpose.


Armin
 
B

Branco Medeiros

MeAgin said:
I want to maintain few variables available for all the classes within the
system. For example the session key and the logged in user'd ID. How can I
do this? I can have a class with all these as properties. But how can I make
a instance of that classes available everywhere?
<snip>

Put an instance of your class in a Module:

<aircode>
Module Globals
Private mState As New AppState

Public ReadOnly Property State As AppState
Get
Return mState
End Get
End Property
...
End Module
</aircode>

In the above example, the State property will be available to the
entire application.

HTH.

Regards,

Branco.
 
G

Guest

I want to maintain few variables available for all the classes within
the system. For example the session key and the logged in user'd ID.
How can I do this? I can have a class with all these as properties.
But how can I make a instance of that classes available everywhere?

You can use Shared (Static) variables or the My.Applications.Settings
class.
 
J

Juan Romero

You can add a module to your project and expose these values either as
variables or as properties.

You can also add a class with shared properties.

Adding a module is basically the same as adding a class since the compiler
will silently create the class with shared methods out of the module for you
upon compilation.

Good luck!

JR
 
C

Cor Ligthert [MVP]

Branco,
In the above example, the State property will be available to the
entire application.

Nice, what can I add, Maybe:

To make your program good maintable you can use this as:
Globals.State

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top