where and how should i declare public variables in a multi-form project?
Printable View
where and how should i declare public variables in a multi-form project?
place them in a module (.bas)
thank you
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!!
i don't like public variables too much, but i'm supposed to use them in this project (damn hairy VB teacher!)
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 ;) )
about variables
From someone on this forum:
Quote:
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
Yeah and then show them how the end result is very well organised but runs 3000 times slower :pQuote:
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 ;) )
Public variables....
Unfortunately a necessary evil in any extensive application...
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????