alien Posted April 6, 2006 Posted April 6, 2006 (edited) :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 Edited April 6, 2006 by alien Quote
Cags Posted April 6, 2006 Posted April 6, 2006 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? Quote Anybody looking for a graduate programmer (Midlands, England)?
alien Posted April 6, 2006 Author Posted April 6, 2006 please gimme a code snippet showing how i can pass a variable from one form to another.vs 2003 Quote
mskeel Posted April 6, 2006 Posted April 6, 2006 public const double EARTH_EQUATORIAL_RADIUS = 6378137;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. Quote
mskeel Posted April 6, 2006 Posted April 6, 2006 Sorry...you posted while I was editing a reply. Check out this post for passing values between forms. Quote
alien Posted April 7, 2006 Author Posted April 7, 2006 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 Quote
Cags Posted April 7, 2006 Posted April 7, 2006 You need to pass the variable, as described in the link mskeel posted. Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.