Global variable  
Author Message
sledge149





PostPosted: Thu Jan 05 16:10:09 CST 2006 Top

Visual Basic >> Global variable

Is it possible to declare global (public) variable conditionally using
If...Then statement?

Visual Studio277  
 
 
Ken





PostPosted: Thu Jan 05 16:10:09 CST 2006 Top

Visual Basic >> Global variable

> Is it possible to declare global (public) variable conditionally using
> If...Then statement?

You can use conditional compilation... like

#If SomeConditional = True Then
Public MyGlobal As String
#End If

But that only works before compiling. (iow, no way to change it once
compiled)

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm


 
 
Joseph





PostPosted: Thu Jan 05 16:16:46 CST 2006 Top

Visual Basic >> Global variable

> Is it possible to declare global (public) variable conditionally using
> If...Then statement?

There is no problem with declaring it globally and setting it to either
a default value or changing the value conditionally when your program
starts execution.

Public GlobalVariable as VariableType

Sub Main()

If Condition = Value Then
GlobalVariable = Value1
Else
GlobalVariable = Value2
End If

End Sub

HTH!

Joseph

 
 
Kevin





PostPosted: Thu Jan 05 17:52:02 CST 2006 Top

Visual Basic >> Global variable Global variables = bad. Properties = good.

- Kev




> Is it possible to declare global (public) variable conditionally using
> If...Then statement?
>


 
 
erewhon





PostPosted: Fri Jan 06 03:17:17 CST 2006 Top

Visual Basic >> Global variable On Thu, 5 Jan 2006 18:52:02 -0500, "Kevin Provance"


>Global variables = bad. Properties = good.

Just above Sub Main()

Private Type TGLB
DataPath As String
UserName As String
...
End Type

Public g As TGLB

That way nobody can create a variable of type TGLB out side that BAS
module

And you get: g.DataPath which is very handy

As is the Intellisense

The UDT can be converted into a Class with no effect on the rest of
the App - if necessary
 
 
Jeff





PostPosted: Fri Jan 06 16:09:33 CST 2006 Top

Visual Basic >> Global variable



> Global variables = bad. Properties = good.

Blanket statements = bad. The right tool for the right job = good.


 
 
Dan





PostPosted: Fri Jan 06 16:54:40 CST 2006 Top

Visual Basic >> Global variable


>


>
>> Global variables = bad. Properties = good.
>
> Blanket statements = bad. The right tool for the right job = good.

Bingo!

Dan


 
 
Jim





PostPosted: Fri Jan 06 18:45:27 CST 2006 Top

Visual Basic >> Global variable


>


>
> > Global variables = bad. Properties = good.
>
> Blanket statements = bad. The right tool for the right job = good.
>
>

For a minute I was thinking I was "bad" for using global variables. Thanks
Jeff.

Jim Edgar


 
 
Kevin





PostPosted: Fri Jan 06 18:58:50 CST 2006 Top

Visual Basic >> Global variable Well, FWIW...it was an MVP who told me in so many words global variables
were evil.

Who to believe? :)

- Kev




>


>
>> Global variables = bad. Properties = good.
>
> Blanket statements = bad. The right tool for the right job = good.
>


 
 
Bob





PostPosted: Fri Jan 06 19:40:39 CST 2006 Top

Visual Basic >> Global variable
> Well, FWIW...it was an MVP who told me in so many words global variables
> were evil.

Being a (current) MVP will very soon mean essentially nothing in this
particular community.


Bob
(of of the very last of the non-dotnet VB MVPs)
--
 
 
MikeD





PostPosted: Fri Jan 06 20:18:28 CST 2006 Top

Visual Basic >> Global variable


> Well, FWIW...it was an MVP who told me in so many words global variables
> were evil.
>

Are you referring to me by any chance? <g>

I've frequently recommended against global variables and always will. I
don't know that I've ever called them evil. They're just very easily misused
and so can get you into trouble (creating hard to find bugs, etc.). I
*definately* think they should be avoided because there are almost always
better alternatives (maybe using properties but also maybe just a more
restrictive scope).

Global variables, in and of themselves, are not evil. It's the misuse of
them that's evil. The problem is that they tend to get misused a LOT. But if
a situation or circumstance truly warrants a global variable, then use it.
You just gotta be careful.

Just for an example: I've taken over applications where there are literally
dozens of global variables for no reason because the variable is only used
in 1 or 2 modules. In the case of 1 module, there's absolutely no reason for
it to be global (but it's probably not hurting anything either). In the
case of 2 or more modules, the programmer needs to consider if it's best for
the variable to be global, or to pass it to procedures/methods or assign it
to properties as necessary.

--
Mike
Microsoft MVP Visual Basic


 
 
Kevin





PostPosted: Fri Jan 06 20:37:58 CST 2006 Top

Visual Basic >> Global variable Of course! However, you are probably correct about the "evil" label. That
was probably something Ken Malone said...when he didn't like something, it
was evil. Especially anything that looked like "Dim xx as New SomeClass".

But yet, I remember very clearly being warded away from global variables in
the PDV days...especially since I was using them frequently. Funny...after
moving them all to properties in one form or another, a lot of my problems
went away. <g>

- Kev




>


>> Well, FWIW...it was an MVP who told me in so many words global variables
>> were evil.
>>
>
> Are you referring to me by any chance? <g>
>
> I've frequently recommended against global variables and always will. I
> don't know that I've ever called them evil. They're just very easily
> misused and so can get you into trouble (creating hard to find bugs,
> etc.). I *definately* think they should be avoided because there are
> almost always better alternatives (maybe using properties but also maybe
> just a more restrictive scope).
>
> Global variables, in and of themselves, are not evil. It's the misuse of
> them that's evil. The problem is that they tend to get misused a LOT. But
> if a situation or circumstance truly warrants a global variable, then use
> it. You just gotta be careful.
>
> Just for an example: I've taken over applications where there are
> literally dozens of global variables for no reason because the variable is
> only used in 1 or 2 modules. In the case of 1 module, there's absolutely
> no reason for it to be global (but it's probably not hurting anything
> either). In the case of 2 or more modules, the programmer needs to
> consider if it's best for the variable to be global, or to pass it to
> procedures/methods or assign it to properties as necessary.
>
> --
> Mike
> Microsoft MVP Visual Basic
>
>


 
 
Kevin





PostPosted: Fri Jan 06 20:39:43 CST 2006 Top

Visual Basic >> Global variable > Being a (current) MVP will very soon mean essentially nothing in this
> particular community.

So I've been told multiple times...which sucks. Personally I refuse to be
assimilated by the .net collective, even if it means never earning the MVP.

- Kev


 
 
Randy





PostPosted: Fri Jan 06 21:05:12 CST 2006 Top

Visual Basic >> Global variable Problem is that for old-time developers, who used QB4 and PDS, almost any
usefulness for apps developed with those tool required a couple of screens
of global variables. That may not be the case today, but there are
situations where they work well without the unnecessary overhead of classes.
For example, every one of my projects (personal, not the ones I publish on
VBnet), use a global UDT called system (sys.* in use) which holds app-wide
settings - form startup position, globally-accessed variables, all the nuts
and bolts that routines have to reference to make decisions. Yes this could
be a class, but why bother? In this case the UDT works like a charm. I do,
however take issue with those (and I've met two so far), that take globals
to the extreme such that these two declared their for/next counter at the
global level, then wondered why the app didn't work properly. <sigh>

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.






:


: > Well, FWIW...it was an MVP who told me in so many words global variables
: > were evil.
: >
:
: Are you referring to me by any chance? <g>
:
: I've frequently recommended against global variables and always will. I
: don't know that I've ever called them evil. They're just very easily
misused
: and so can get you into trouble (creating hard to find bugs, etc.). I
: *definately* think they should be avoided because there are almost always
: better alternatives (maybe using properties but also maybe just a more
: restrictive scope).
:
: Global variables, in and of themselves, are not evil. It's the misuse of
: them that's evil. The problem is that they tend to get misused a LOT. But
if
: a situation or circumstance truly warrants a global variable, then use it.
: You just gotta be careful.
:
: Just for an example: I've taken over applications where there are
literally
: dozens of global variables for no reason because the variable is only used
: in 1 or 2 modules. In the case of 1 module, there's absolutely no reason
for
: it to be global (but it's probably not hurting anything either). In the
: case of 2 or more modules, the programmer needs to consider if it's best
for
: the variable to be global, or to pass it to procedures/methods or assign
it
: to properties as necessary.
:
: --
: Mike
: Microsoft MVP Visual Basic
:
:

 
 
richmann





PostPosted: Sat Jan 07 19:58:31 CST 2006 Top

Visual Basic >> Global variable

>


>> Well, FWIW...it was an MVP who told me in so many words global variables
>> were evil.
>>
>
>Are you referring to me by any chance? <g>
>
>I've frequently recommended against global variables and always will. I
>don't know that I've ever called them evil. They're just very easily misused
>and so can get you into trouble (creating hard to find bugs, etc.). I
>*definately* think they should be avoided because there are almost always
>better alternatives (maybe using properties but also maybe just a more
>restrictive scope).
>
>Global variables, in and of themselves, are not evil. It's the misuse of
>them that's evil. The problem is that they tend to get misused a LOT. But if
>a situation or circumstance truly warrants a global variable, then use it.
>You just gotta be careful.

I personnaly use them only for CONST and for error values constants
with descriptive names.
Also some ENUMS for application wide argument passing.

>Just for an example: I've taken over applications where there are literally
>dozens of global variables for no reason because the variable is only used
>in 1 or 2 modules. In the case of 1 module, there's absolutely no reason for
>it to be global (but it's probably not hurting anything either). In the
>case of 2 or more modules, the programmer needs to consider if it's best for
>the variable to be global, or to pass it to procedures/methods or assign it
>to properties as necessary.
>
>--
>Mike
>Microsoft MVP Visual Basic
>
>

**********************************************************************


Dog thinks: they feed me, they take care of me: they are gods.
Cat thinks: they feed me, they take care of me: I am god.
http://www3.sympatico.ca/richmann/
http://www.geocities.com/richmannsoft/
**********************************************************************
 
 
Penelopa





PostPosted: Sun Jan 08 16:26:02 CST 2006 Top

Visual Basic >> Global variable Thank you all for responses, but I am not smarter when I was before :(
So, if global variables are evil, what should I do?
My project has 5 forms and 7 modules.
Some of my global variables are used in ALL forms and modules. How should I
make it differently?
I am completely at the loss right now.
Penelopa



> Is it possible to declare global (public) variable conditionally using
> If...Then statement?
>


 
 
Jan





PostPosted: Mon Jan 09 03:22:36 CST 2006 Top

Visual Basic >> Global variable
released on Fri, 06 Jan 2006 17:40:39 -0800 bearing the
following fruit:


>> Well, FWIW...it was an MVP who told me in so many words global variables
>> were evil.
>
>Being a (current) MVP will very soon mean essentially nothing in this
>particular community.

Why?





Jan Hyde (VB MVP)

--
Did you hear about the guy who blamed arithmetic for his divorce?
His wife put two and two together.

(Art. Moger)

 
 
Bob





PostPosted: Mon Jan 09 13:07:21 CST 2006 Top

Visual Basic >> Global variable

> released on Fri, 06 Jan 2006 17:40:39 -0800 bearing the
> following fruit:
>
>

>>
>>>Well, FWIW...it was an MVP who told me in so many words global variables
>>>were evil.
>>
>>Being a (current) MVP will very soon mean essentially nothing in this
>>particular community.
>
>
> Why?


Because nothing was ever said "officially" that I'm aware of, I'm going to
preface this with "it is my understanding that..."


The "powers that be" have directed those who give the awards to NOT re-award
certain still-HUGELY-contributing VB MVPs, specifically because those have
NOT taken up dotnet. I'm not even sure whether I may be the ONLY non-dotnet
VB-only MVP who has current status, and that may be only because my award
anniversary date differs from most, or because I've been less-visibly
critical of late. In another ten months, I'm fairly certain there won't be
even one VB MVP - apart from those who've taken up dotnet.

In any event, whether one's body of contributions are good for the community
is no longer sufficient to be awarded as an MVP. They have to be good for
the Microsoft Party Line as well. Even though this specific community still
thrives, we've been abandoned - just like our code assets.



Bob
 
 
Rick





PostPosted: Mon Jan 09 13:34:46 CST 2006 Top

Visual Basic >> Global variable > I'm not even sure whether I may be the ONLY
> non-dotnet VB-only MVP who has current status,
> and that may be only because my award
> anniversary date differs from most,

I'm an October awardee (which is the "normal" anniversary date) with current
status who does not use or want to move down<g> to VB.NET.

Rick


 
 
Karl





PostPosted: Mon Jan 09 14:00:06 CST 2006 Top

Visual Basic >> Global variable
>>> Being a (current) MVP will very soon mean essentially nothing in
>>> this particular community.
>>
>> Why?
>
> Because nothing was ever said "officially" that I'm aware of, I'm
> going to preface this with "it is my understanding that..."
<snip>
> In any event, whether one's body of contributions are good for the
> community is no longer sufficient to be awarded as an MVP.

Unequivacably true statement, that.
--
Be the 10,000th Signer!
http://classicvb.org


 
 
Dan





PostPosted: Mon Jan 09 16:33:08 CST 2006 Top

Visual Basic >> Global variable



>>>> Being a (current) MVP will very soon mean essentially nothing in
>>>> this particular community.
>>>
>>> Why?
>>
>> Because nothing was ever said "officially" that I'm aware of, I'm
>> going to preface this with "it is my understanding that..."
> <snip>
>> In any event, whether one's body of contributions are good for the
>> community is no longer sufficient to be awarded as an MVP.
>
> Unequivacably true statement, that.

Thanks for finally joining<evilG>.

I figure it won't be long for the rest.

Dan ([the/one of the] first MVP.Nots)



 
 
Randy





PostPosted: Mon Jan 09 18:34:55 CST 2006 Top

Visual Basic >> Global variable Hi Bob ... I've not taken the kool-aid either, and this is my 10th year.
Even though I love a **** has much as the next guy, I don't believe
dot-nettedness has anything to do with it.

--

Randy Birch
MS MVP Visual Basic
http://www.hide-link.com/

Please reply to the newsgroups so all can participate.








: > released on Fri, 06 Jan 2006 17:40:39 -0800 bearing the
: > following fruit:
: >
: >

: >>
: >>>Well, FWIW...it was an MVP who told me in so many words global
variables
: >>>were evil.
: >>
: >>Being a (current) MVP will very soon mean essentially nothing in this
: >>particular community.
: >
: >
: > Why?
:
:
: Because nothing was ever said "officially" that I'm aware of, I'm going to
: preface this with "it is my understanding that..."
:
:
: The "powers that be" have directed those who give the awards to NOT
re-award
: certain still-HUGELY-contributing VB MVPs, specifically because those have
: NOT taken up dotnet. I'm not even sure whether I may be the ONLY
non-dotnet
: VB-only MVP who has current status, and that may be only because my award
: anniversary date differs from most, or because I've been less-visibly
: critical of late. In another ten months, I'm fairly certain there won't
be
: even one VB MVP - apart from those who've taken up dotnet.
:
: In any event, whether one's body of contributions are good for the
community
: is no longer sufficient to be awarded as an MVP. They have to be good for
: the Microsoft Party Line as well. Even though this specific community
still
: thrives, we've been abandoned - just like our code assets.
:
:
:
: Bob

 
 
Bob





PostPosted: Mon Jan 09 23:49:40 CST 2006 Top

Visual Basic >> Global variable
> Hi Bob ... I've not taken the kool-aid either, and this is my 10th year.
> Even though I love a **** has much as the next guy, I don't believe
> dot-nettedness has anything to do with it.
>


As in essentially every prediction I've made during the end-of-VB debacle,
I **hope** that I'm wrong. I guess we'll all learn in October.



Bob
--
 
 
Kevin





PostPosted: Tue Jan 10 00:41:34 CST 2006 Top

Visual Basic >> Global variable > As in essentially every prediction I've made during the end-of-VB debacle,
> I **hope** that I'm wrong. I guess we'll all learn in October.

<hint> Maybe if someone nominates me, we can find out! <g> </hint>

- Kev


 
 
Jan





PostPosted: Tue Jan 10 03:10:48 CST 2006 Top

Visual Basic >> Global variable
released on Mon, 09 Jan 2006 11:07:21 -0800 bearing the
following fruit:



>> released on Fri, 06 Jan 2006 17:40:39 -0800 bearing the
>> following fruit:
>>
>>

>>>
>>>>Well, FWIW...it was an MVP who told me in so many words global variables
>>>>were evil.
>>>
>>>Being a (current) MVP will very soon mean essentially nothing in this
>>>particular community.
>>
>>
>> Why?
>
>
>Because nothing was ever said "officially" that I'm aware of, I'm going to
>preface this with "it is my understanding that..."
>
>The "powers that be" have directed those who give the awards to NOT re-award
>certain still-HUGELY-contributing VB MVPs, specifically because those have
>NOT taken up dotnet. I'm not even sure whether I may be the ONLY non-dotnet
>VB-only MVP who has current status, and that may be only because my award
>anniversary date differs from most, or because I've been less-visibly
>critical of late. In another ten months, I'm fairly certain there won't be
>even one VB MVP - apart from those who've taken up dotnet.

I was re-awarded this week. Although there is now no way to
distiguish between VB.Net MVPs and VB MVPs, I'm reasonably
certain (since I'm still here) that there is still a place
for us VB 'classic' MVPs.

>In any event, whether one's body of contributions are good for the community
>is no longer sufficient to be awarded as an MVP. They have to be good for
>the Microsoft Party Line as well. Even though this specific community still
>thrives, we've been abandoned - just like our code assets.

I don't think that's true, I think it is harder to get
awarded for contributions soley to VB 'classic' simply
because these groups are pretty well covered. I logged on
this morning and there isn't much for me to add, most
questions have already been answered.






>
> Bob


Jan Hyde (VB MVP)

--
What is the religion of a woman who had a sex-change operation?
A He Then

(Stan Kegel)

 
 
Ken





PostPosted: Tue Jan 10 10:15:48 CST 2006 Top

Visual Basic >> Global variable

>
>
> Jan Hyde (VB MVP)
>
> --
> What is the religion of a woman who had a sex-change operation?
> A He Then

Groan......... <g>

> (Stan Kegel)



--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm


 
 
Penelopa





PostPosted: Tue Jan 10 11:55:41 CST 2006 Top

Visual Basic >> Global variable How about going back to vbasic boring problems???
Please see the last post in this thread.





>>
>>
>> Jan Hyde (VB MVP)
>>
>> --
>> What is the religion of a woman who had a sex-change operation?
>> A He Then
>
> Groan......... <g>
>
>> (Stan Kegel)
>
>
>
> --
> Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
>


 
 
Penelopa





PostPosted: Tue Jan 10 11:54:13 CST 2006 Top

Visual Basic >> Global variable Really????
Please see to the bottom of this thread.
My question is still not replied to, although it was me who stirred this pot
:-(




> released on Mon, 09 Jan 2006 11:07:21 -0800 bearing the
> following fruit:
>


>>> released on Fri, 06 Jan 2006 17:40:39 -0800 bearing the
>>> following fruit:
>>>
>>>

>>>>
>>>>>Well, FWIW...it was an MVP who told me in so many words global
>>>>>variables
>>>>>were evil.
>>>>
>>>>Being a (current) MVP will very soon mean essentially nothing in this
>>>>particular community.
>>>
>>>
>>> Why?
>>
>>
>>Because nothing was ever said "officially" that I'm aware of, I'm going to
>>preface this with "it is my understanding that..."
>>
>>The "powers that be" have directed those who give the awards to NOT
>>re-award
>>certain still-HUGELY-contributing VB MVPs, specifically because those have
>>NOT taken up dotnet. I'm not even sure whether I may be the ONLY
>>non-dotnet
>>VB-only MVP who has current status, and that may be only because my award
>>anniversary date differs from most, or because I've been less-visibly
>>critical of late. In another ten months, I'm fairly certain there won't
>>be
>>even one VB MVP - apart from those who've taken up dotnet.
>
> I was re-awarded this week. Although there is now no way to
> distiguish between VB.Net MVPs and VB MVPs, I'm reasonably
> certain (since I'm still here) that there is still a place
> for us VB 'classic' MVPs.
>
>>In any event, whether one's body of contributions are good for the
>>community
>>is no longer sufficient to be awarded as an MVP. They have to be good for
>>the Microsoft Party Line as well. Even though this specific community
>>still
>>thrives, we've been abandoned - just like our code assets.
>
> I don't think that's true, I think it is harder to get
> awarded for contributions soley to VB 'classic' simply
> because these groups are pretty well covered. I logged on
> this morning and there isn't much for me to add, most
> questions have already been answered.
>
>
>
>
>
>
>>
>> Bob
>
>
> Jan Hyde (VB MVP)
>
> --
> What is the religion of a woman who had a sex-change operation?
> A He Then
>
> (Stan Kegel)
>


 
 
Mike





PostPosted: Tue Jan 10 12:14:00 CST 2006 Top

Visual Basic >> Global variable


> My question is still not replied to, although it was
> me who stirred this pot :-(

Well I've just tried to figure out a way to do it and I haven't managed it
yet. I've only been trying for five minutes or so and there may be a way
that I have missed, but it isn't looking promising so far. I'd like to know
why you wouyld want to do this though? What purpose could you have? I mean,
declaring a variable (public or otherwise) doesn't require many bytes of
memory, and then you can use (or not use) or redimension or whatever when
you feel the need. What, specifically, are you after, and (often more
importantly) why are you after it?

Mike


 
 
Bob





PostPosted: Tue Jan 10 18:18:00 CST 2006 Top

Visual Basic >> Global variable
> How about going back to vbasic boring problems???
> Please see the last post in this thread.

global variables are NOT "evil"

They're just a tool, like many others, which is extremely frequently used
very badly. For the tiny few uses where they're among the right answers,
it's stupid to try to avoid them, especially for what amounts to nothing
more than "religious" reasons.


-->end of thread.



Bob
 
 
Jan





PostPosted: Wed Jan 11 03:39:20 CST 2006 Top

Visual Basic >> Global variable
on Sun, 8 Jan 2006 17:26:02 -0500 bearing the following
fruit:

>Thank you all for responses, but I am not smarter when I was before :(
>So, if global variables are evil, what should I do?
>My project has 5 forms and 7 modules.
>Some of my global variables are used in ALL forms and modules. How should I
>make it differently?
>I am completely at the loss right now.
>Penelopa

Personally I avoid global variables.

All my forms have an InitForm routine, here I pass in the
minimum requirements that form needs to operate. This
usually is not much.

Since many of my forms are shared with other projects this
works very well. If they had been global variables then I
would have had to set up these same global variables in my
other projects.

I like to create objects when I need them and destroy them
when I don't.

It's easy to rely on global variables for small projects but
later on when you start sharing code between projects, or
your project suddenly grows in size then you may regret it.

You will *never* get an answer to your question because we
don't know your project and even if we did you'd never get
us all to agree.

My advice is that if you can avoid using them then do so -
getting into the habit of doing this means you rarely even
ask the question you've raised, you just do it.

As many have said, the misuse of global variables is what is
really bad. Having said that, if you have global variable
then sooner or later they will be abused. Either because of
someone else working on your code, or because of time
constraints you do a hack job than you never get round to
putting right.








>> Is it possible to declare global (public) variable conditionally using
>> If...Then statement?
>>
>


Jan Hyde (VB MVP)

--
Every morning is the dawn of a new error. (Don Thorn)