When to use Public,private , shared etc

  • Thread starter Thread starter Neil Steventon
  • Start date Start date
N

Neil Steventon

Hi,

I am new to VB.net , well the objects concept anyway and was wondering if
there were any good articles / turorials on when and why you would use
certain declarations such as

Private test as string
public test as string
Public Shared as string
Protected test as string

etc

Please could someone she some light, am I right in thinking if I develop an
MDI with lots of child forms would it be better off creating a module with a
variable of

Public Shared dbconn as String

in order to allow all forms in project access to this variable ?

Thank alot

Neil
 
MSDN is a good source for discussing the accessability systax.

In general I stay away from declaring Public variables if possible because
they take up space on the stack and are hard to debug (ie. any piece of code
can change the value of dbconn from anywhere), but sometimes as in your case
it can be a good solution over declaring a conn string in every form.

Also there is probably no reason to declare dbconn as Shared since it will
be in a module inside your app.

Dan
 
Hey solex,

don't mean to rag on you, but thats a really bad explanation of a public
variable. You describe a shared variable basically, public just means any
member can see the public member and execute/read or whatever function it
serves.

Understanding accessors is a pretty large task, especially when you start
getting into inheritance (where you will see protected a lot). But the MSDN
is a good source as solex said.

But like I said, not trying to rag, but it is a little misleading.

-CJ
 
In addition to what the guys have told you here. I suggest that you sit down
with some reference material, either from help or the microsoft site etc and
experiment.

Access modifiers specify either where you can instantiate an object or how
you can access it or its members.

Experimentation will re-enforce your understanding greatly. Try it, your not
going to break much.

Regards - OHM
 
Thanks for reply's - info taken onboard

CJ Taylor said:
Hey solex,

don't mean to rag on you, but thats a really bad explanation of a public
variable. You describe a shared variable basically, public just means any
member can see the public member and execute/read or whatever function it
serves.

Understanding accessors is a pretty large task, especially when you start
getting into inheritance (where you will see protected a lot). But the MSDN
is a good source as solex said.

But like I said, not trying to rag, but it is a little misleading.

-CJ
 
No offense taken CJ.

I should have said a Public variable in a Module (as the original poster
suggested) take up space on the stack and are difficult to debug.

Thanks,

Dan
 
Back
Top