473,883 Members | 1,741 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 2732
>
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=tru e" 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
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******** ******@TK2MSFTN GP09.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
> 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
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****@mindspr ing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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=tru e" 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
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="Tru e" 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.nosp am> wrote in message
news:uJ******** ******@tk2msftn gp13.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****@mindspr ing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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=tru e" 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
Joisey,

See responses inline:

<jo****@mindspr ing.com> wrote in message
news:Om******** ******@TK2MSFTN GP09.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="Tru e" 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.nosp am> wrote in message
news:uJ******** ******@tk2msftn gp13.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****@mindspr ing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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=tru e" 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
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.....retu rn" out of my blood! LOL.

thanx....joisey


Nov 21 '05 #27
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****@mindspr ing.com> wrote in message
news:ul******** ******@TK2MSFTN GP09.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.....retu rn" out of my blood! LOL.

thanx....joisey


Nov 21 '05 #28

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

Similar topics

2
1731
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
1900
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
1775
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
2088
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
2507
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
2086
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
4697
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
3816
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
9796
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
11153
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
10757
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
10860
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
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9583
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...
0
7134
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
6002
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4620
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.