Results 1 to 8 of 8

Thread: Declaring global variables

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Posts
    34

    Lightbulb

    How can you declare global variables that can be used throughout all the forms in a project?

    An answer would be nice, thanks,
    Wick

  2. #2
    Junior Member
    Join Date
    Jun 2000
    Posts
    27
    declare the global variables in the Moudle
    KraMba

  3. #3
    Addicted Member
    Join Date
    Jan 2000
    Location
    Oshkosh, WI
    Posts
    163
    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.
    Glenn D
    Development/Analyst

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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

  5. #5
    Member
    Join Date
    Jul 2000
    Location
    Eastern Universe
    Posts
    32
    You should add a module to 'ur project
    and have x variable declaration in there.

    -----------------------------------------

    oooO Knock! Knock! Ooooo

    -----------------------------------------

  6. #6
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    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
    If you don't declare the type, doesn't it default to a variant data type (which is horribly slower than other declared types)?

  7. #7
    Addicted Member
    Join Date
    Jan 2000
    Location
    Oshkosh, WI
    Posts
    163
    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.

    Glenn D
    Development/Analyst

  8. #8
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width