By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,682 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,682 IT Pros & Developers. It's quick & easy.

Public Variables

P: n/a

Variables that I would like to make available to all forms and modules in my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way to
do this?

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
well i believe you do it just right , i also use a module for that , however
with one slight twist i use Friend instead of public in VB.Net modules

regards

M. Posseth [MCP]

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .

Variables that I would like to make available to all forms and modules in
my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way to
do this?

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #2

P: n/a

Thanks for the quick answer. I have been using VB2005 for about a month
now. I have never used Friend, but I will look it up and read about it.

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #3

P: n/a
You declare them in a standard Module and make that Module the project's
startup item.

Friend level scope makes the item accessible anywhere IN the assembly.
Public scope makes the item accessible anywhere INSIDE or from OUTSIDE the
assembly.
<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .

Thanks for the quick answer. I have been using VB2005 for about a month
now. I have never used Friend, but I will look it up and read about it.

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #4

P: n/a
> You declare them in a standard Module and make that Module the project's
startup item.

Scott M.,

Please correct me if I am wrong, but I am not sure if you *have* to set
the module as the start up object. That information might be misleading
to the OP. In my opinion, even if you do not set a module as a start up
object, and it is not an object module, meaning it is a standard
module, then the variable declared as public in that module will be
accessible globally throughout.

The scope of accessibility, of course, as you mentioned, will be global
if it is public and assembly scope, if it is friend.

Regards,
Sathyaish

Nov 21 '05 #5

P: n/a
You are correct that the standard Module need not be set as the Startup
Object in the project. I still recommend that it be set that way, as it is
more self-documenting in terms of programmatic flow.

"Sathyaish" <sa*******@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
You declare them in a standard Module and make that Module the project's

startup item.

Scott M.,

Please correct me if I am wrong, but I am not sure if you *have* to set
the module as the start up object. That information might be misleading
to the OP. In my opinion, even if you do not set a module as a start up
object, and it is not an object module, meaning it is a standard
module, then the variable declared as public in that module will be
accessible globally throughout.

The scope of accessibility, of course, as you mentioned, will be global
if it is public and assembly scope, if it is friend.

Regards,
Sathyaish

Nov 21 '05 #6

P: n/a
This might have been de-rigeur in the days of VB6 but VB.NET is an object
oriented language and there is no good excuse for a public variable anywhere
in such a system. They cause endless problems and should be avoided like the
plague.

Variables should always be private and accessed through correctly structured
get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to provide
methods and properties that are globally available. These should however be
completely self-contained and not rely on any stored data such as a static
method referring to a static property and other such bad habits. The Math
class is the perfect example of where the Shared keyword is properly used.

If you want to develop good OOP techniques throw out just about everything
you learned from VB6.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .

Variables that I would like to make available to all forms and modules in
my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way to
do this?

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #7

P: n/a
"Scott M." <s-***@nospam.nospam> schrieb:
You are correct that the standard Module need not be set as the Startup
Object in the project. I still recommend that it be set that way, as it
is more self-documenting in terms of programmatic flow.


In this case I prefer selecting 'Sub Main' as startup object. However, the
decision is up tp personal preference...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8

P: n/a
I have a form (Form2) which has a lot of overhead when it first loads so I
have a module containing sub main from which I start my application. In this
module, I declare a global variable for Form2 as below since it's shown as a
dialog in several places in my "mainform" as well as from otherr forms that I
show as dialogs from "mainform"

public myForm2 as Form2

sub main
'Show Splash screen
myForm2 = new Form2
'Close Splash screen
mainform.show
Application.Run()
end sub

I find using the global variable myForm2 a lot easier and faster than trying
to declare the myForm2 in "mainform" then passing it as a property to other
forms that use myForm2 that are called as dialogs from "mainform"

How would you do it and yet retain the speed of only having to load Form2
once while a splash screen is showing.
--
Dennis in Houston
"Bob Powell [MVP]" wrote:
This might have been de-rigeur in the days of VB6 but VB.NET is an object
oriented language and there is no good excuse for a public variable anywhere
in such a system. They cause endless problems and should be avoided like the
plague.

Variables should always be private and accessed through correctly structured
get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to provide
methods and properties that are globally available. These should however be
completely self-contained and not rely on any stored data such as a static
method referring to a static property and other such bad habits. The Math
class is the perfect example of where the Shared keyword is properly used.

If you want to develop good OOP techniques throw out just about everything
you learned from VB6.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .

Variables that I would like to make available to all forms and modules in
my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way to
do this?

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Nov 21 '05 #9

P: n/a
This is where "Friend" level scope becomes useful. Public means that the
variable is available from anywhere within the assembly
as well as by external assmeblies that reference the first one.

Friend level scope means that the variable can be called by any code within
the assembly only.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
I have a form (Form2) which has a lot of overhead when it first loads so I
have a module containing sub main from which I start my application. In
this
module, I declare a global variable for Form2 as below since it's shown as
a
dialog in several places in my "mainform" as well as from otherr forms
that I
show as dialogs from "mainform"

public myForm2 as Form2

sub main
'Show Splash screen
myForm2 = new Form2
'Close Splash screen
mainform.show
Application.Run()
end sub

I find using the global variable myForm2 a lot easier and faster than
trying
to declare the myForm2 in "mainform" then passing it as a property to
other
forms that use myForm2 that are called as dialogs from "mainform"

How would you do it and yet retain the speed of only having to load Form2
once while a splash screen is showing.
--
Dennis in Houston
"Bob Powell [MVP]" wrote:
This might have been de-rigeur in the days of VB6 but VB.NET is an object
oriented language and there is no good excuse for a public variable
anywhere
in such a system. They cause endless problems and should be avoided like
the
plague.

Variables should always be private and accessed through correctly
structured
get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to provide
methods and properties that are globally available. These should however
be
completely self-contained and not rely on any stored data such as a
static
method referring to a static property and other such bad habits. The Math
class is the perfect example of where the Shared keyword is properly
used.

If you want to develop good OOP techniques throw out just about
everything
you learned from VB6.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .
>
> Variables that I would like to make available to all forms and modules
> in
> my
> program, where should I declare them? At the momment I just created a
> module and have them all declared public there. What is the normal way
> to
> do this?
>
> Thanks,
>
> Thomas
>
> --
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Nov 21 '05 #10

P: n/a
On 2005-09-04, Dennis <De****@discussions.microsoft.com> wrote:
I have a form (Form2) which has a lot of overhead when it first loads so I
have a module containing sub main from which I start my application. In this
module, I declare a global variable for Form2 as below since it's shown as a
dialog in several places in my "mainform" as well as from otherr forms that I
show as dialogs from "mainform"
The standard OOP answer to this is that you want a Singleton. Given
that it's probably the most well-known of the standard design patterns,
it's pretty easy to find documentation on it all over the Net.

The only caveat I'd add is that you don't want lazy initialization.
Access the singleton during Sub Main to ensure initialization.
I find using the global variable myForm2 a lot easier and faster


Yep, globals are fast and easy. They also pollute the global namespace,
provide no access control, and have no way of ensuring single
initialization.

I don't really have strong feelings on these things (well, I do, but I
don't expect others to). If you work by yourself or on a small team
with relatively small projects, you can break every rule in the book
and it doesn't matter. After all, *you'll* probably remember not to
reinitialize Form2. However, I do think it's useful for developers to
know that these are standard problems and that lots of smart people have
standardized on common solutions to these problems that provide ease of
maintenance and expandibility.


Nov 21 '05 #11

P: n/a
Scott,
You are correct that the standard Module need not be set as the Startup
Object in the project. I still recommend that it be set that way, as it
is more self-documenting in terms of programmatic flow.


I prefer a MainForm as start object, because that is in my opinion the most
self-documenting in terms of program flow, especially because most
documentation is based on that.

However I respect your opinion in this of course, just showing that in fact
it is a matter of preference.

Cor
Nov 21 '05 #12

P: n/a
> If you want to develop good OOP techniques throw out just about everything
you learned from VB6.
well read this ....

http://www.geocities.com/tablizer/oopbad.htm

http://www.devx.com/opinion/Article/26776

OOP brought some good things ,,, but it sure isn`t "the" way you must
program in for all situations

in some situations you need global scoped variabels
I do not wish to limit my programs to OOP rules , i strive to find the most
elegant and fastest solutions and if this might not be OOP compatible then
so be it
regards

Michel Posseth [MCP]

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Oh**************@TK2MSFTNGP10.phx.gbl... This might have been de-rigeur in the days of VB6 but VB.NET is an object
oriented language and there is no good excuse for a public variable
anywhere in such a system. They cause endless problems and should be
avoided like the plague.

Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to provide
methods and properties that are globally available. These should however
be completely self-contained and not rely on any stored data such as a
static method referring to a static property and other such bad habits.
The Math class is the perfect example of where the Shared keyword is
properly used.

If you want to develop good OOP techniques throw out just about everything
you learned from VB6.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .

Variables that I would like to make available to all forms and modules in
my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way
to
do this?

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Nov 21 '05 #13

P: n/a
Bob,

I agree with you, although there is mostly memory enough is it for me always
a waste to keep it useless in memory.

For that is the GC an very good addition to the Net software. However you
have to give the GC it change by creating your program in a way that it can
do its work. By placing everything on the main stack makes in my opinion all
the work done for the GC completely a waste of time.

Therefore instancing there where you need things is for me the best method.

If you need other values, just pass them (in whatever way, because by
instance you can pass it as a from a class created object what can be an
immense quantity of data/references) or get the reference from an existing
reference that is in scoop.

Just my thought,

Cor
Nov 21 '05 #14

P: n/a
Thanks for the links to those two articles. I needed a good laugh.

I have some links to articles that have solid proof that the Earth has
already been invaded by aliens and the the world is hollow and inhabited by
superbeings that are benevolently controlling our atmosphere. Like the
articles you cite, niether of them have a basis in reality and are not
wrtten by credible authors who's industry experience would lead me to change
my opinions based upon their veracity.

You only need global scoped variables if you don't understand how to do the
job properly.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"m.posseth" <mi*****@nohausystems.nl> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
If you want to develop good OOP techniques throw out just about
everything you learned from VB6.


well read this ....

http://www.geocities.com/tablizer/oopbad.htm

http://www.devx.com/opinion/Article/26776

OOP brought some good things ,,, but it sure isn`t "the" way you must
program in for all situations

in some situations you need global scoped variabels
I do not wish to limit my programs to OOP rules , i strive to find the
most elegant and fastest solutions and if this might not be OOP compatible
then so be it
regards

Michel Posseth [MCP]

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Oh**************@TK2MSFTNGP10.phx.gbl...
This might have been de-rigeur in the days of VB6 but VB.NET is an object
oriented language and there is no good excuse for a public variable
anywhere in such a system. They cause endless problems and should be
avoided like the plague.

Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to provide
methods and properties that are globally available. These should however
be completely self-contained and not rely on any stored data such as a
static method referring to a static property and other such bad habits.
The Math class is the perfect example of where the Shared keyword is
properly used.

If you want to develop good OOP techniques throw out just about
everything you learned from VB6.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .

Variables that I would like to make available to all forms and modules
in my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way
to
do this?

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



Nov 21 '05 #15

P: n/a
Are all global variables kept on the stack or are global reference types kept
on the managed heap and value types on the stack?
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:
Bob,

I agree with you, although there is mostly memory enough is it for me always
a waste to keep it useless in memory.

For that is the GC an very good addition to the Net software. However you
have to give the GC it change by creating your program in a way that it can
do its work. By placing everything on the main stack makes in my opinion all
the work done for the GC completely a waste of time.

Therefore instancing there where you need things is for me the best method.

If you need other values, just pass them (in whatever way, because by
instance you can pass it as a from a class created object what can be an
immense quantity of data/references) or get the reference from an existing
reference that is in scoop.

Just my thought,

Cor

Nov 21 '05 #16

P: n/a
On 2005-09-04, Dennis <De****@discussions.microsoft.com> wrote:
Are all global variables kept on the stack or are global reference types kept
on the managed heap and value types on the stack?


Actually they're both on the heap. The stack vs. heap thing really only
refers to local variables.

Also, remember that in a sense there aren't really any *true* global
variables in VB.Net. Modules are just classes internally. The VB
compiler just keeps track of their names and adds the correct class
specifier internally when you reference them.
Nov 21 '05 #17

P: n/a
Well happy i got someone laughing again this weekend , ;-)
You only need global scoped variables if you don't understand how to do
the job properly.
ouch that hurts ...... well i do know how to do it , but sometimes i feel it
makes my programs unnecesary complex

isn`t the purpose of OOP to reduce the cognitive load on the programming
team and not to make code more efficient ?
regards

Michel Posseth


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:em**************@TK2MSFTNGP14.phx.gbl... Thanks for the links to those two articles. I needed a good laugh.

I have some links to articles that have solid proof that the Earth has
already been invaded by aliens and the the world is hollow and inhabited
by superbeings that are benevolently controlling our atmosphere. Like the
articles you cite, niether of them have a basis in reality and are not
wrtten by credible authors who's industry experience would lead me to
change my opinions based upon their veracity.

You only need global scoped variables if you don't understand how to do
the job properly.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"m.posseth" <mi*****@nohausystems.nl> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
If you want to develop good OOP techniques throw out just about
everything you learned from VB6.


well read this ....

http://www.geocities.com/tablizer/oopbad.htm

http://www.devx.com/opinion/Article/26776

OOP brought some good things ,,, but it sure isn`t "the" way you must
program in for all situations

in some situations you need global scoped variabels
I do not wish to limit my programs to OOP rules , i strive to find the
most elegant and fastest solutions and if this might not be OOP
compatible then so be it
regards

Michel Posseth [MCP]

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Oh**************@TK2MSFTNGP10.phx.gbl...
This might have been de-rigeur in the days of VB6 but VB.NET is an
object oriented language and there is no good excuse for a public
variable anywhere in such a system. They cause endless problems and
should be avoided like the plague.

Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to
provide methods and properties that are globally available. These should
however be completely self-contained and not rely on any stored data
such as a static method referring to a static property and other such
bad habits. The Math class is the perfect example of where the Shared
keyword is properly used.

If you want to develop good OOP techniques throw out just about
everything you learned from VB6.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .

Variables that I would like to make available to all forms and modules
in my
program, where should I declare them? At the momment I just created a
module and have them all declared public there. What is the normal way
to
do this?

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



Nov 21 '05 #18

P: n/a
Michael,

As I read it now, can people get from you (slightly) and others the idea
that it is usable unlimited and even is better to do that. I disagree with
those (I am sure not with you) who tell that.

If a possibility is there, than there is a reason for it, is always my
opinion. .

However, global variables/objects should in my opinion be used with care,
they are hard to find back after a while and by trying avoiding them you get
an in my opinion better program.

Just my thought,

Cor
Nov 21 '05 #19

P: n/a
Seriously...

OOP is there for several reasons. Firstly to make the re-use of code easy
and secondly to provide a way of encapsulating data within the code that
controls it. Any globally accessible variable bypasses the second, and
incidentally the most important, reason for OOP and enables any bit of code
to change the data without the knowledge or permission of the class that is
supposed to be looking after it. This is not acceptable in a correctly
engineered system.

As has been pointed out. The model your particular problem seems to call for
is a singleton. This is a well known and sensible pattern that fits the OOP
architecture while still giving the ability to provide a single point of
access for the data. The singleton is an instance, controls it's own data
and still obeys the rules of encapsulation if correctly engineered.

The cognitive load on the programmer isn't the point here. It's the
cognitive load on the poor sod who has to come along in two years time and
unravel the horrible spaghetti code with global variables that change
inexplicably from moment to moment after you've been recruited to some other
job.

Good program design doesn't happen by accident. Cock-ups do.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"m.posseth" <mi*****@nohausystems.nl> wrote in message
news:ef**************@TK2MSFTNGP14.phx.gbl...
Well happy i got someone laughing again this weekend , ;-)
You only need global scoped variables if you don't understand how to do
the job properly.


ouch that hurts ...... well i do know how to do it , but sometimes i feel
it makes my programs unnecesary complex

isn`t the purpose of OOP to reduce the cognitive load on the programming
team and not to make code more efficient ?
regards

Michel Posseth


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:em**************@TK2MSFTNGP14.phx.gbl...
Thanks for the links to those two articles. I needed a good laugh.

I have some links to articles that have solid proof that the Earth has
already been invaded by aliens and the the world is hollow and inhabited
by superbeings that are benevolently controlling our atmosphere. Like the
articles you cite, niether of them have a basis in reality and are not
wrtten by credible authors who's industry experience would lead me to
change my opinions based upon their veracity.

You only need global scoped variables if you don't understand how to do
the job properly.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"m.posseth" <mi*****@nohausystems.nl> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
If you want to develop good OOP techniques throw out just about
everything you learned from VB6.

well read this ....

http://www.geocities.com/tablizer/oopbad.htm

http://www.devx.com/opinion/Article/26776

OOP brought some good things ,,, but it sure isn`t "the" way you must
program in for all situations

in some situations you need global scoped variabels
I do not wish to limit my programs to OOP rules , i strive to find the
most elegant and fastest solutions and if this might not be OOP
compatible then so be it
regards

Michel Posseth [MCP]

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Oh**************@TK2MSFTNGP10.phx.gbl...
This might have been de-rigeur in the days of VB6 but VB.NET is an
object oriented language and there is no good excuse for a public
variable anywhere in such a system. They cause endless problems and
should be avoided like the plague.

Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to
provide methods and properties that are globally available. These
should however be completely self-contained and not rely on any stored
data such as a static method referring to a static property and other
such bad habits. The Math class is the perfect example of where the
Shared keyword is properly used.

If you want to develop good OOP techniques throw out just about
everything you learned from VB6.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<th*****@msala.net> wrote in message
news:43**********************@news.newsdemon.com.. .
>
> Variables that I would like to make available to all forms and modules
> in my
> program, where should I declare them? At the momment I just created a
> module and have them all declared public there. What is the normal
> way to
> do this?
>
> Thanks,
>
> Thomas
>
> --
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



Nov 21 '05 #20

P: n/a
>
Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to provide
methods and properties that are globally available. These should however
be completely self-contained and not rely on any stored data such as a
static method referring to a static property and other such bad habits.
The Math class is the perfect example of where the Shared keyword is
properly used.

If you want to develop good OOP techniques throw out just about everything
you learned from VB6.
Bob Powell [MVP]
Visual C#, System.Drawing


I have never used VB6. I purchased VB.Net for the purpose of writing a
game management program.

The first thing the progam does is read-in and store about 20 pieces of
information
about the game configuration files.
These are storred in variables, mostly as true/false but some as integers.

The program consists of 2 forms, and about 12 routines I wrote.
Almost all of these routines need to be able to check the state of any or
all of these
variables. If a routine changes a variable, The next routine called MUST
know the
current state of any or all of the variables.

While writing the program I got sick and tired of constantly being warned
that these
variable had not been declared. I was repeatedly told to add dim statements.

I finally moved ALL of these variables to the "Main" module as PUBLIC!!!

If the ONLY PURPOSE of the program is to ascertain if 20 conditions are true
or false,
and if EVERY (repeat EVERY) subroutine needs access to this information, are
you
asking me pass all 20 back and forth for every single subroutine????

If my progam discovers that a certain configuration file "exists" and I set
the flag
"FileExists=true" why the hell should I declare this variable private so
that not even one
of the routines I write can check this?

How much routine should be written to protect the access to a variable in a
2 form
program that is only 4 pages long?

.....joisey







Nov 21 '05 #21

P: n/a
I actually prefer to start with a form myself as well. My point was that
*if* you are going to use a Standard Module, then that will become the start
up object by default, so in that case making it be set as the start up
object makes sense.
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
Scott,
You are correct that the standard Module need not be set as the Startup
Object in the project. I still recommend that it be set that way, as it
is more self-documenting in terms of programmatic flow.


I prefer a MainForm as start object, because that is in my opinion the
most self-documenting in terms of program flow, especially because most
documentation is based on that.

However I respect your opinion in this of course, just showing that in
fact it is a matter of preference.

Cor

Nov 21 '05 #22

P: n/a
> isn`t the purpose of OOP to reduce the cognitive load on the programming
team and not to make code more efficient ?


No, not really. OOP is meant to make code more hierarchical and reusable.
In may cases, OOP means adding more code layers and structure, which can
actually reduce efficiency, but create more robust code and so that is a
worthwhile trade off.
Nov 21 '05 #23

P: n/a
The answer to your question is "go learn what OOP programming is and you
will understand" because there is way to much to explain at the level you
seem to be at.
<jo****@mindspring.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to provide
methods and properties that are globally available. These should however
be completely self-contained and not rely on any stored data such as a
static method referring to a static property and other such bad habits.
The Math class is the perfect example of where the Shared keyword is
properly used.

If you want to develop good OOP techniques throw out just about
everything you learned from VB6.
Bob Powell [MVP]
Visual C#, System.Drawing


I have never used VB6. I purchased VB.Net for the purpose of writing a
game management program.

The first thing the progam does is read-in and store about 20 pieces of
information
about the game configuration files.
These are storred in variables, mostly as true/false but some as integers.

The program consists of 2 forms, and about 12 routines I wrote.
Almost all of these routines need to be able to check the state of any or
all of these
variables. If a routine changes a variable, The next routine called MUST
know the
current state of any or all of the variables.

While writing the program I got sick and tired of constantly being warned
that these
variable had not been declared. I was repeatedly told to add dim
statements.

I finally moved ALL of these variables to the "Main" module as PUBLIC!!!

If the ONLY PURPOSE of the program is to ascertain if 20 conditions are
true or false,
and if EVERY (repeat EVERY) subroutine needs access to this information,
are you
asking me pass all 20 back and forth for every single subroutine????

If my progam discovers that a certain configuration file "exists" and I
set the flag
"FileExists=true" why the hell should I declare this variable private so
that not even one
of the routines I write can check this?

How much routine should be written to protect the access to a variable in
a 2 form
program that is only 4 pages long?

....joisey







Nov 21 '05 #24

P: n/a
Scott:
Are you saying that there is no circumstance in which a person should ever
declare a variable as public?
Are you stating that I should ALWAYS declare ALL variables as private?
Variables should always be private and accessed through correctly
structured get/set accessor properties.

That seems like a lot of work for 20 variables in a one module program of
only 1500 lines.

My object is not to learn programming style as I only wrote this one program
because no comercial programs
existed that performed this task. My only interest is to get code that
works.

I come to this news group because when I have had questions on how to solve
specific problems, many people
have come to my assistance.

(The little utility I wrote has been purchased by over 600 people and used
for almost a year and with no complaints.)

I would still like to know:
Since almost every procedure in my program contains the line If
FileExists="True" then......etc etc etc,
why I should not declare 'FileExists' as a public string at the top of the
Main module?
If one of the proceedures changes the value to "false", why would I make
this private to that proceedure,
when every other proceedure needs to know the new value.

thanx......
......joisey






"Scott M." <s-***@nospam.nospam> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
The answer to your question is "go learn what OOP programming is and you
will understand" because there is way to much to explain at the level you
seem to be at.


<jo****@mindspring.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... >
Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to
provide methods and properties that are globally available. These should
however be completely self-contained and not rely on any stored data
such as a static method referring to a static property and other such
bad habits. The Math class is the perfect example of where the Shared
keyword is properly used.

If you want to develop good OOP techniques throw out just about
everything you learned from VB6.
Bob Powell [MVP]
Visual C#, System.Drawing


I have never used VB6. I purchased VB.Net for the purpose of writing a
game management program.

The first thing the progam does is read-in and store about 20 pieces of
information
about the game configuration files.
These are storred in variables, mostly as true/false but some as
integers.

The program consists of 2 forms, and about 12 routines I wrote.
Almost all of these routines need to be able to check the state of any or
all of these
variables. If a routine changes a variable, The next routine called MUST
know the
current state of any or all of the variables.

While writing the program I got sick and tired of constantly being warned
that these
variable had not been declared. I was repeatedly told to add dim
statements.

I finally moved ALL of these variables to the "Main" module as PUBLIC!!!

If the ONLY PURPOSE of the program is to ascertain if 20 conditions are
true or false,
and if EVERY (repeat EVERY) subroutine needs access to this information,
are you
asking me pass all 20 back and forth for every single subroutine????

If my progam discovers that a certain configuration file "exists" and I
set the flag
"FileExists=true" why the hell should I declare this variable private so
that not even one
of the routines I write can check this?

How much routine should be written to protect the access to a variable in
a 2 form
program that is only 4 pages long?

....joisey








Nov 21 '05 #25

P: n/a
Joisey,

See responses inline:

<jo****@mindspring.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
Scott:
Are you saying that there is no circumstance in which a person should ever
declare a variable as public?
If you wanted to really follow the OOP paradigm, then probably not. The
whole premis of OOP is that each object be a "black box" of code and that
only code IN the black box can see the rest of the code IN the black box.
If the black box wanted to expose certain functions or data, the box should
have Public methods and Properties.
Are you stating that I should ALWAYS declare ALL variables as private?
Variables should always be private and accessed through correctly
structured get/set accessor properties.

That seems like a lot of work for 20 variables in a one module program of
only 1500 lines.


Again, yes! You could make a class that exposes the 20 data fields as
Public properties.

My object is not to learn programming style as I only wrote this one
program because no comercial programs
existed that performed this task. My only interest is to get code that
works.
Ok fine. But, you asked your question in a forum full of people that do
care about programming style.
I come to this news group because when I have had questions on how to
solve specific problems, many people
have come to my assistance.
If you are saying that you come here to get answers that don't include good
OOP style and concepts (and that you have gotten just that in the past), I
would simply say you should then start your posts off with "I'm not
interested in OOP programming style - I just need a way to [fill in question
here] that will work."
(The little utility I wrote has been purchased by over 600 people and used
for almost a year and with no complaints.)
Great. But, why would a user complain about something that they will never
be able to see directly?

I never complained about my old 25" color television, but when I got a 42"
plasma, I realized that my other TV sucked by comparison.

Also, what will you do when you decide to release a new version of your
program and you'll want to add more features (which almost always happens)?
As your program grows, it will undoubtably become more complex. Building
something "properly" in the beginning makes it easier to add on to later.
I would still like to know:
Since almost every procedure in my program contains the line If
FileExists="True" then......etc etc etc,
why I should not declare 'FileExists' as a public string at the top of the
Main module?
If one of the proceedures changes the value to "false", why would I make
this private to that proceedure,
when every other proceedure needs to know the new value.
Again, if we are keeping this conversation to best OOP practices, you would
want to create a class with a Shared property that exposes your "FileExists"
Boolean data. If we are not, then what you've done "works" just fine.

thanx......
.....joisey






"Scott M." <s-***@nospam.nospam> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
The answer to your question is "go learn what OOP programming is and you
will understand" because there is way to much to explain at the level you
seem to be at.



<jo****@mindspring.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
>
Variables should always be private and accessed through correctly
structured get/set accessor properties.

In certain, very limited cases, you can use the shared keyword to
provide methods and properties that are globally available. These
should however be completely self-contained and not rely on any stored
data such as a static method referring to a static property and other
such bad habits. The Math class is the perfect example of where the
Shared keyword is properly used.

If you want to develop good OOP techniques throw out just about
everything you learned from VB6.
Bob Powell [MVP]
Visual C#, System.Drawing

I have never used VB6. I purchased VB.Net for the purpose of writing a
game management program.

The first thing the progam does is read-in and store about 20 pieces of
information
about the game configuration files.
These are storred in variables, mostly as true/false but some as
integers.

The program consists of 2 forms, and about 12 routines I wrote.
Almost all of these routines need to be able to check the state of any
or all of these
variables. If a routine changes a variable, The next routine called MUST
know the
current state of any or all of the variables.

While writing the program I got sick and tired of constantly being
warned that these
variable had not been declared. I was repeatedly told to add dim
statements.

I finally moved ALL of these variables to the "Main" module as PUBLIC!!!

If the ONLY PURPOSE of the program is to ascertain if 20 conditions are
true or false,
and if EVERY (repeat EVERY) subroutine needs access to this information,
are you
asking me pass all 20 back and forth for every single subroutine????

If my progam discovers that a certain configuration file "exists" and I
set the flag
"FileExists=true" why the hell should I declare this variable private so
that not even one
of the routines I write can check this?

How much routine should be written to protect the access to a variable
in a 2 form
program that is only 4 pages long?

....joisey









Nov 21 '05 #26

P: n/a
Scot: wrote:
>> Also, what will you do when you decide to release a new version of your
program and you'll want to add more features (which almost always happens)?
As your program grows, it will undoubtedly become more complex. Building
something "properly" in the beginning makes it easier to add on to later.>>


Scot, I am beginning to see you are correct!
They say "well begun is half done", and I am already looking at a new
more complex revision.

I never used VB6.
I came directly to VB.net with only the definition of a project and 6 weeks
to
complete it, while still holding down my day-job. (The programmer I planned
to use bailed out on me) I bought VB.net and 4 books, and with the help
of this forum made the release date on time.

(I looked at books on both C and VB, and found I could relate better to VB.
I had not written any code since 1986. I wrote a lot of utilities for the
1602,
(Atari 400,800,1200) using assembly, Basic, and Action!. I have some
knowledge of FORTRAN and "Logo". You really had to plan when everything
(compiler, program, data, screen memory, stacks) had to fit into just 16K
!!!

I guess it's just hard to get "gosub.....return" out of my blood! LOL.

thanx....joisey


Nov 21 '05 #27

P: n/a
Good luck to you but I will say that I started out with BASIC myself and it
took a few years to just get line numbers out of my head (Read & Data / List
& Run, etc.).

<jo****@mindspring.com> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
Scot: wrote:
>>> Also, what will you do when you decide to release a new version of your
program and you'll want to add more features (which almost always
happens)?
As your program grows, it will undoubtedly become more complex. Building
something "properly" in the beginning makes it easier to add on to later.>>>


Scot, I am beginning to see you are correct!
They say "well begun is half done", and I am already looking at a new
more complex revision.

I never used VB6.
I came directly to VB.net with only the definition of a project and 6
weeks to
complete it, while still holding down my day-job. (The programmer I
planned
to use bailed out on me) I bought VB.net and 4 books, and with the help
of this forum made the release date on time.

(I looked at books on both C and VB, and found I could relate better to
VB.
I had not written any code since 1986. I wrote a lot of utilities for the
1602,
(Atari 400,800,1200) using assembly, Basic, and Action!. I have some
knowledge of FORTRAN and "Logo". You really had to plan when everything
(compiler, program, data, screen memory, stacks) had to fit into just 16K
!!!

I guess it's just hard to get "gosub.....return" out of my blood! LOL.

thanx....joisey


Nov 21 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.