Groups | Blog | Home
all groups > vb.net > march 2005 >

vb.net : Static Variables


Nick Malik [Microsoft]
3/5/2005 10:46:21 PM
all objects of the same class share the static values. The static value is
a property of the class itself, not of any instance of the class.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
[quoted text, click to view]

J L
3/5/2005 10:58:56 PM
Hi Nick,
Does that mean that Static variables are shared..i.e. can be set
without creating an instance of the class?

TIA
John

On Sat, 5 Mar 2005 22:46:21 -0800, "Nick Malik [Microsoft]"
[quoted text, click to view]
Nick Malik [Microsoft]
3/5/2005 11:04:41 PM
yes

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
[quoted text, click to view]

Raghu
3/5/2005 11:31:28 PM
A local variable in a procedure can be declared as Static so that it can
retain value after the procedure ends. My question is: Does the variable
retain value among multiple objects of same type? Or is it specific to one
object instance?

Thanks.

Nick Malik [Microsoft]
3/6/2005 1:40:08 AM
good catch, Cor.

I'm a C# person. I made the same mistake.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
[quoted text, click to view]

J L
3/6/2005 7:03:44 AM

Thanks Nick, Cor and Herfried,
It is clear now but was confusing befre.

John

On Sun, 6 Mar 2005 11:15:37 +0100, "Herfried K. Wagner [MVP]"
[quoted text, click to view]
Cor Ligthert
3/6/2005 8:43:27 AM
JL,


[quoted text, click to view]

No

(The static keyword is often mixed up because the Static keyword in C# is
the same as the Shared keyword in VBNet. The reason from those different
names, is probably because there exist in the VB language a "Static"
Variable what is a special keyword).

Where the answer in Nick's first answer stays the same by the way.

Reading your question in a little different way. "Does that mean that Shared
variables are static....", than the answer on your last question would be
Yes, crazy enough.

I have tried to make it clear using this sample for you.

\\\
Public Class Class1
Public Shared Sub main()
Class1A.a = 1
Class1A.b = 1 'This is not possible
End Sub
End Class
Public Class Class1A
Public Shared a As Integer = 0
Public Shared Sub main()
Static b As Integer = 0
Static Shared c As Integer = 0 'This is not allowed
End Sub
End Class
///

I hope this helps?

Cor

Herfried K. Wagner [MVP]
3/6/2005 11:15:37 AM
"Raghu" <Raghu@Nospam.com> schrieb:
[quoted text, click to view]


If the method they are declared in is not a shared method, they will belong
to the instances.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
AddThis Social Bookmark Button