How can you declare global variables that can be used throughout all the forms in a project?
An answer would be nice, thanks,
Wick
Printable View
How can you declare global variables that can be used throughout all the forms in a project?
An answer would be nice, thanks,
Wick
declare the global variables in the Moudle
Declare them in the declarations section of a code module like:
Global x as integer
BTW, you should proabably post this type of question in the General VB forum.
You don't need the as integer part. But its a good idea.
If its going to always change (like some of mine do) then you can just say
Global x
You should add a module to 'ur project
and have x variable declaration in there.
If you don't declare the type, doesn't it default to a variant data type (which is horribly slower than other declared types)?Quote:
Originally posted by SteveCRM
You don't need the as integer part. But its a good idea.
If its going to always change (like some of mine do) then you can just say
Global x
JHausmann you are correct (give that man a cigar :)). Undeclared variables are treated as variants. They tend to be slower because VB has to figure out the storage at runtime, instead of compile time. They also waste memory, because VB allocates memory for the largest possible data type.
Variants are not considered good programming practice for the above reasons. They also should a design weakness (the programmer doesn't know what to store ? :( ) There are legitimate uses for variants. Such as null values in databases or when passing optional paramaters to functions and subroutines.
However I try to avoid them at all costs.
Just my 2 cents worth.
:):)
You can get them if you're not careful with your Dims. Consider:
Dim a,b,c as integer
only c will be an integer, a and b will be created as variants