Re: Public Variables
- From: "Scott M." <s-mar@xxxxxxxxxxxxx>
- Date: Mon, 5 Sep 2005 08:45:18 -0400
Joisey,
See responses inline:
<joisey@xxxxxxxxxxxxxx> wrote in message
news:OmzDfTdsFHA.2592@xxxxxxxxxxxxxxxxxxxxxxx
> 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-mar@xxxxxxxxxxxxx> wrote in message
> news:uJwa0TcsFHA.3604@xxxxxxxxxxxxxxxxxxxxxxx
>> 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.
>>
>
>
>>
>> <joisey@xxxxxxxxxxxxxx> wrote in message
>> news:%230HZcMbsFHA.1960@xxxxxxxxxxxxxxxxxxxxxxx
>>> >
>>>> 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
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
.
- Follow-Ups:
- Re: Public Variables
- From: joisey
- Re: Public Variables
- References:
- Public Variables
- From: thomasp
- Re: Public Variables
- From: Bob Powell [MVP]
- Re: Public Variables
- From: joisey
- Re: Public Variables
- From: Scott M.
- Re: Public Variables
- From: joisey
- Public Variables
- Prev by Date: Re: Multithreading problem
- Next by Date: Re: Upgrade to .net 2
- Previous by thread: Re: Public Variables
- Next by thread: Re: Public Variables
- Index(es):
Relevant Pages
|