RE: Shared Static Variables in vb.net please? by asuwb
asuwb
Wed Aug 18 21:49:04 CDT 2004
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
""Peter Huang"" wrote:
> 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.
>
>