Shared Static Variables in vb.net please?

G

Guest

Does anyone know if MS has / is planning to introduce a shared static local
variable to VB.net? I have had a few instances now where I thought this would
be a tidy way to write some code.

i.e. an instance method needs to access a shared (class) variable, and it is
the only method that needs to access it.

I really like the concept of static variables in vb.net, they make class
definitions a lot tidier I think. Just a shame they called the keyword
"static" as seems to confuse people.

Cheers.
 
J

Jonathan Allen

I don't see a need for it. What would be the benefit over a normal shared
variable?

Jonathan
 
G

Guest

Although there is no "need" for it in the sense that there is no missing
functionality, I think it would be useful to have these variables effectively
"hidden" from the rest of the class if they are of no relevance (to other
methods / properties in the class).

For an example of a benefit, the Intellisense->List Members list would be
shorter for a given class.

- Lachlan
 
S

Scott M.

Why wouldn't marking the variable that only a particular method needs access
to as "private"? It would not show up in the intelliSense for users of the
class.
 
S

Scott M.

You would not see it listed after me. in an instance because it is marked as
private. You would see it listed if it were marked as protected or public,
but not as private. Private members are only visible to the class not
instances of the class. "Me" refers to the current instance.
 
P

Peter Huang

Hi Asuwb,

Based on my understanding, you want to have a static variable which can
only be access from one method. Is the below one what you wants?
Inside the method Inc, the "s_var" static variable can only be accessed by
using the Inc method. If we specified the a class variable as shared then
it will be a static one too, which will be access from all the method
inside the class. So the two keywords static and shared defined different
access scope, one on the procedure level and the other on the module/class
level. So I am not sure what you want to do is the procedure level or class
level? If I have any misunderstanding, can you describe your goal more
detailed I will appreciate it

Module Module1
Sub Main()
Dim o As New TestClass
For i As Integer = 0 To 10
o.Inc()
Next
End Sub
End Module
Public Class TestClass
Public Sub Test()
'Console.WriteLine(s_var)
'We can not access the s_var here.
End Sub
Public Sub Inc()
Static s_var As Integer
s_var = s_var + 1
Console.WriteLine(s_var)
End Sub
End Class

I look forward to hearing from you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Private, as a class level member scope, refers to an instance unless "shared"
is specified.
 
G

Guest

No I understand how to do that.

What I want to do is this, to produce output of 1 - 20:

i.e. the equivalent of creating a private shared class-level variable, but
visible only within a single method.

Module Module1
Sub Main()
Dim o As New TestClass
Dim o2 as new TestClass
For i As Integer = 0 To 10
o.Inc()
Next
For i as integer = 0 To 10
o2.Inc()
Next
End Sub
End Module
Public Class TestClass
Public Sub Test()
'Console.WriteLine(s_var)
'We can not access the s_var here.
End Sub
Public Sub Inc()
Static SHARED s_var As Integer
s_var = s_var + 1
Console.WriteLine(s_var)
End Sub
End Class

- Lachlan
 
P

Peter Huang

Hi Asuwb,

I understand your meaning, but I think a class variable is at the class
level so it will be visible in the class level, i.e. it can be seen by all
the class method, if we define a static variable at the procedure level,
then it belongs to the procedure not the class level. I think this is by
design.

For you scenario, I think we can define the Inc method as shared, so that
it will hold one copy per class in the memory.
Sub Main()
Dim o As New TestClass
Dim o2 As New TestClass
For i As Integer = 0 To 10
o.Inc()
Next
For i As Integer = 0 To 10
o2.Inc()
Next
End Sub
Public Class TestClass
Private Shared i As Integer
Public Sub Test()
'Console.WriteLine(s_var)
'We can not access the s_var here.
End Sub
Public Shared Sub Inc()
Static s_var As Integer
s_var = s_var + 1
i = i + 1
Console.WriteLine(i)
Console.WriteLine(s_var)
End Sub
End Class

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Yes I want the functionality of a static variable in a shared method (i.e.
class level variable only visible in one method) BUT from an instance method
(may deal with a mixture of instance and class variables). This desire came
about because I wanted to create a shared method in an interface, which VS
tells me I can't do.

Can we consider the subject closed now, obviously there is not the support I
thought there might be.

- Lachlan
 
P

Peter Huang

Hi

If you still have any question, please feel free to post here.
Thanks for your understanding.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top