variable??????????

alien

Newcomer
Joined
Mar 21, 2006
Messages
24
:p hi
is it possible to declare a variable in such a way that it maintains its value even in other forms.
iam using vs.net 2003
 
Last edited:
I believe you can do this in VB using a module (not used VB.Net much, but thats how you did it in 6.0). It can be achieved in C# also using the static key word. It's generally a good idea to avoid this as much as possible though. Is it possible to get around this global need by passing the variable to any Form or object that requires it?
 
please gimme a code snippet showing how i can pass a variable from one form to another.vs 2003
 
C#:
public const double EARTH_EQUATORIAL_RADIUS = 6378137;
Visual Basic:
Public Const EARTH_EQUATORIAL_RADIUS As Double = 6378137
The const keyword is static in nature but requires that a value be given at compile time. The value is unchangeable throughout the life of the program. Using static (C#) or shared (VB) will give you access to the variable in the same way as const (ClassName.VariableName) but the value can change at runtime.

In VB, if you use shared or const no module is required. You can declare const and shared/static variables in any class in both C# and VB and have access to them the same way, across other classes.

A quick note on style and usage. Generally const is reserved for values that are truly constant such as pi, Earth's radius, and Planck's constant (and it's not always numbers :)). For most cases, you'd be better off passing values as Cags suggested.

Const may not be what you are looking for but something declared const certainly won't change values between forms/classes and will be accessibly fairly widely.
 
the variable am tryin to use keeps changing depending on the circumstances, but i need this variable to be passed onto another form or class.declaring as as constant doesnt seem to solve the problem. i do not want to use a module since i dont understand modules well in vb.net2003. any other ideas? :p
 
Back
Top