Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by alien
Posted
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?
Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

Posted
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
Posted
You need to pass the variable, as described in the link mskeel posted.
Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...