Results 1 to 10 of 10

Thread: public variables

  1. #1

    Thread Starter
    Member mafia_geek's Avatar
    Join Date
    Jan 2002
    Location
    California
    Posts
    45

    public variables

    where and how should i declare public variables in a multi-form project?
    -mafia_geek-

    Condito, Ergo Sum

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    place them in a module (.bas)
    -= a peet post =-

  3. #3

    Thread Starter
    Member mafia_geek's Avatar
    Join Date
    Jan 2002
    Location
    California
    Posts
    45
    thank you

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497
    Conservatives would say avoid public variables at all costs. Them aside, you should add a basic module to your project and declare public members there.

    For small projects that have a kind of single use, single purpose existence, I don't think public members are so bad. Like if your project has two or three forms and has one basic function like generating a report or creating a file or calculating some number.

    If the project grows, however, it gets harder and hard to keep track of those public variables. If your project's purpose changes, sometimes the global variables lose their purpose as well or become multipurpose--which is even worse. The freedom of declaring an object public can become the entrapment of not knowing where that object has been!!
    end war
    stop greed

  5. #5

    Thread Starter
    Member mafia_geek's Avatar
    Join Date
    Jan 2002
    Location
    California
    Posts
    45
    i don't like public variables too much, but i'm supposed to use them in this project (damn hairy VB teacher!)

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Impress your teacher by mentioning what vbmom said. You should also tell him/her that you believe that adding properties and/or methods to a Class is better (but you'd better understand how to do that )

  7. #7
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    about variables

    From someone on this forum:
    A variable DIM'med inside any sub or function (in any module or form)
    is local to that routine only (only known about by the dim'ming routine),
    and is destroyed upon exiting that routine.


    A variable DIM'med as Private in the general declarations section of
    a Form is available only to routines in that form. The variable remains
    valid as long as the form is loaded.


    A variable DIM'med as Public in the general declarations section of a
    Form (i.e. Public Foo As Long) is available to routines in that form by
    referencing the variable name (i.e. Foo), or to modules or forms other than
    the declaring form by explicitly referring to the variable with its formname
    as a prefix (i.e. Form1.Foo). The variable remains valid as long
    as the form is loaded.


    A variable DIM'med Private in the general declarations section of a BAS
    module is available routines only in that bas module. The Private keyword
    is not available in VB2 or 3; use a standard DIM statement.


    A variable DIM'med Public (or Global in VB2 or 3) in the general
    declarations section of a BAS module is available to all routines in all
    forms and bas modules.


    A variable DIM'med inside any sub or function that has the same name as
    a Public or Global variable takes precedence over the Public one in that
    routine where it is DIM'med. Its initial value is 0 or empty. The Public
    variable's contents is not affected by use of the local variable of the same
    name. The local variable is destroyed upon exiting that routine; the global variable
    remains valid. Download a VB4 demo and view the code. Generally, using the
    same name for a Public and Local variable is considered poor programming practice
    ______________

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by MartinLiss
    Impress your teacher by mentioning what vbmom said. You should also tell him/her that you believe that adding properties and/or methods to a Class is better (but you'd better understand how to do that )
    Yeah and then show them how the end result is very well organised but runs 3000 times slower
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington, DC
    Posts
    422
    Public variables....

    Unfortunately a necessary evil in any extensive application...

  10. #10
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    Stop this bull****. Public Variables are all over the place in VB and all other programming languages.

    Use 'em, don't use 'em? Depends on the task at hand. Nothing is right, nothing is wrong.

    Get the point????

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