473,836 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Public Variables


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.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05
27 2725
On 2005-09-04, Dennis <De****@discuss ions.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
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
> 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@_spamkille r_bobpowell.net > wrote in message
news:Oh******** ******@TK2MSFTN GP10.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.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Nov 21 '05 #13
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
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*****@nohaus ystems.nl> wrote in message
news:ut******** ******@TK2MSFTN GP09.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@_spamkille r_bobpowell.net > wrote in message
news:Oh******** ******@TK2MSFTN GP10.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.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



Nov 21 '05 #15
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
On 2005-09-04, Dennis <De****@discuss ions.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
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@_spamkille r_bobpowell.net > wrote in message
news:em******** ******@TK2MSFTN GP14.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*****@nohaus ystems.nl> wrote in message
news:ut******** ******@TK2MSFTN GP09.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@_spamkille r_bobpowell.net > wrote in message
news:Oh******** ******@TK2MSFTN GP10.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.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



Nov 21 '05 #18
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
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*****@nohaus ystems.nl> wrote in message
news:ef******** ******@TK2MSFTN GP14.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@_spamkille r_bobpowell.net > wrote in message
news:em******** ******@TK2MSFTN GP14.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*****@nohaus ystems.nl> wrote in message
news:ut******** ******@TK2MSFTN GP09.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@_spamkille r_bobpowell.net > wrote in message
news:Oh******** ******@TK2MSFTN GP10.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.c om<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



Nov 21 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1728
by: Jose Meireles | last post by:
Hi everyone, I'm trying to use public variables in a web form to hld specific values. What happens is that the public variables (declared as public x as y in the beginning of the class), doesn't seem to hold the value from function call to function call. Does anyone can help me abou this? best regards
6
1899
by: darrel | last post by:
I'm still not quite sure how best to handle the passing of data between controls. This is a method I'm using at the moment: I have an XML file that contains a variety of page-centric variables. I have one control that loads the XML file, reads through it, and grabs the variables it needs. It then sets these to PUBLIC variables. Then, other usercontrols simply request that variable.
3
1773
by: Mrs. Conni Drejer | last post by:
Please help anyone: I have made a DLL (for DBCommunication) in which I have defined some public static variables/fields in a public class. This because I want these variables to be shared in all methods in the total application. Both web- and windowsapplications and other DLL's are using this DLL. When one of the Web-application uses the DLL, my variables are shared across sessions, but I do not want this to happen - I want the variables to...
4
2080
by: Webster | last post by:
Hello, Just wondering what's better programming style; to use public variables in a class or to use private/protected variables and then expose them via properties? For example: -------------------------------- Public Class public_person
4
2503
by: Nick Dreyer | last post by:
Is it possible to see public class variables of a COM addin in Excel 97 VBA? I have successfully created the (Visual Basic 2003 .NET) COM and referenced it in an Excel 97 VBA project. The VBA object browser sees - and the project otherwise successfully interacts with - all the COM addin methods and properties, but none of the public variables - target, init and size in the code below - can be accessed. When I reference the exact same...
7
2083
by: Steve Mauldin | last post by:
I have a public variable that is declared in a public module. This Variable is stored into a Session variable and used to pass data from page to page. I am seeing on my local development box that once the variable is created and loaded with data and stored into the session variable that on the next aspx page, before the first line of the page load is executed, the public variables data persists. I have not done an assignment from the...
86
4672
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
5
3815
by: Web Search Store | last post by:
Hello, I made a web page using visual studio. I also made a public class in the app_code folder called 'allvars' In the main web page durning the page startup, I can refer to public shared variables by simply saying: x=allvars.variable1
3
1642
by: PhilippeM | last post by:
Hi everyone! I have defined 3 public variables: Public OldCompany As Variant Public OldLast As Variant Public OldFirst As Variant A procedure defines those variables: OldCompany = !Company OldLast = !
0
9668
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10843
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10589
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.