Static variables with variable names

  • Thread starter Thread starter Jerry Spence1
  • Start date Start date
J

Jerry Spence1

Sorry for the confusing title - not quite sure what I'm after here.

I think need to have a static variable in a procedure, but the name of that
variable will be designated at run time. Also I don't know how many
variables I need until run time either!

Just to explain, I have a number of IP based door access control readers and
I need to retain a status flag about certain ones - I don't know which ones
until run time. The readers have names. So I might have:

static <my variable> as boolean

which will come out as static Reader_30 as boolean

There may be another way of doing this of course, such as properties.

-Jerry
 
well in this situation i would create a structure that holds the required
info about the status ( boolean , string , integer values about the status
etc etc )
then store these structures in a hashtable or collection ( i prefer the
hashtable as it is the fastest )

now you can save these values with anny name you like , and can even check
for there existence in the lookup table

i could write you an example if you like the idea

regards

Michel Posseth
 
I think I like that idea - but am not very knowledgable on it. If you have
an example I'd be grateful

-Jerry
 
well copy this in a new form

and see the demo :-)

Option Strict On

Option Explicit On

Public Class Form1

Inherits System.Windows.Forms.Form

Private MemTable As New Hashtable

Private Structure AccessReader

Dim Used As Integer

Dim GrantedAccess As Integer

Dim DeniedAccess As Integer

End Structure



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'just a small usage demo

Dim aantal As Integer = 1000

MsgBox("inserting " & aantal.ToString & " readers")

For i As Integer = 0 To aantal

addNewAcessReader("readerid" & i.ToString)

Next

MsgBox("change values of the readers")



For i As Integer = 0 To aantal

updatereader("readerid" & i.ToString, (i Mod 2 = 0))

Next



MsgBox("show all the values in the debug window")



For i As Integer = 0 To aantal

Dim arr As AccessReader = DirectCast(MemTable.Item("readerid" & i.ToString),
AccessReader)

Debug.WriteLine("reader : readerid" & i.ToString)

With arr

Debug.WriteLine("used : " & .Used.ToString)

Debug.WriteLine("granted : " & .GrantedAccess.ToString)

Debug.WriteLine("denied : " & .DeniedAccess.ToString)

End With

Next



MsgBox("finished")



End Sub

Private Function updatereader(ByVal accReaid As String, ByVal AccGranted As
Boolean) As Boolean

If MemTable.ContainsKey(accReaid) Then

Dim arr As AccessReader = DirectCast(MemTable.Item(accReaid), AccessReader)

With arr

..Used += 1

If AccGranted Then

..GrantedAccess += 1

Else

..DeniedAccess += 1

End If

End With

MemTable.Remove(accReaid)

MemTable.Add(accReaid, arr)

Return True

End If

End Function

Private Function addNewAcessReader(ByVal accReaid As String) As Boolean

If Not MemTable.ContainsKey(accReaid) Then

Dim ar As New AccessReader

With ar

..Used = 0

..DeniedAccess = 0

..GrantedAccess = 0

End With

MemTable.Add(accReaid, ar)

Return True

End If

End Function

End Class


i hope you get the idea ,,,, ofcourse you can name your objects anyway you
want however they must be unique in the hashtable


regards

Michel Posseth [MCP]
 
Jerry,

You got your answer from Michel

However your subject and text is real confusing.

In VB Net is a static variable a keyword for a static variable inside a
method

Friend sub A ()
static a as integer
End sub

"a" will hold its last value as long that the class/object exist while it is
not accessible (seems to exist) outside that procedure..

This is a what strange behaviour from Visual Basic, not real OOP, however
sometimes very handy.

Beside this there are in VBNet Global variables, which do the same, however
are accessible to all methods in an object/class as long as that exist and
Shared variables which are in fact a consistent part of the program and
accessible as long as the program exist.

I hope this gives an idea.

Cor
 
Hi Cor

I'm not too sure what you mean and I don't understand "In VB Net is a static
variable a keyword for a static variable inside a method"

What I was referring to (before Michel's reply) as:

Sub Test

static my variable as Boolean

End Sub

Are you referring to my (mis)use of the word Procedure, when I really meant
Subroutine perhaps? Forgive my lack of correct names - formal VB training is
something that seemed to have drifted past me!

Regs

Jerry
 
Jerry,

No than you used it exactly as it is, however probably I got confused what
you was describing. Because what you describe in my opinion just an array
which is global.

Where Michel showed you an hashtable which is an more extended array, which
has an dictionary item with a value so makes it possible to get that value
using the dictionary.

What is "shared" in VBNet has in C# the name "static", so that gives often
confusions.

However your explanation was the right one for VBNet, although I would not
take it as solution for your problem and have a look what Michel showed you
what is outside the procedure however Global in the class.

Because Michels "Private MemTable As New Hashtable" is outside the procedure
is it global for the form, however not for all other classes inside your
application.

So let us say, I was confused.

:-)

Cor
 

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

Back
Top