Static variable?

'Is okay
 Private _HasStandardRights As Boolean = True
'Is okay
Private _SpecialRightsLogic As Boolean = True
'Cannot do this?
Static _HasBeenMadeVisibleOnce As Boolean = False

How do I properly declare a static variable in VB .NET?
Tom KnowltonWeb developerAsked:
Who is Participating?
Alexandre SimõesManager / Technology SpecialistCommented:

Take a look:

Is this what you're looking for? Or are you trying to do recreate the Static behavior of C#?
If so, c# 'static' is named 'Shared' in VB.

Assuming that you are trying to create a static class member, no that is not valid.  Class members are "static" throughout the life of the class object.  You can only declare a static variable inside of a Sub/Function.

Public Class Form1
    Inherits System.Windows.Forms.Form

      'NOT valid
      Static _HasBeenMadeVisibleOnce As Boolean = False

      Private Sub MySub()
            Static _HasBeenMadeVisibleOnce As Boolean = False
      End Sub

End Class
Tom KnowltonWeb developerAuthor Commented: it, I think:

Protected Overrides Sub OnVisibleChanged(ByVal e As System.EventArgs)

        'Tom Knowlton
        'To fix bug 152
        Static _bHasBeenMadeVisibleOnce As Boolean = False

        If Me.Visible = True And _bHasBeenMadeVisibleOnce = False  Then
          _bHasBeenMadeVisibleOnce = True
        End If
    End Sub

My intent is that SearchEquipment   ONLY   fires the first time the form is made visible.

If my approach is not best-practice, I welcome suggestions!!  


Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tom KnowltonWeb developerAuthor Commented:

Yeah, it's different in C#.

Yes, I had in mind to duplicate the same functionality in VB .NET, but it's not a matter of life-and-death that I do so.  I can live with declaring the static variable inside the function as long as it works pretty much like a static variable would in C#.

Using "Shared" sounds like it is closer to what I had in mind  (  originally  )
Tom KnowltonWeb developerAuthor Commented:
Ummm....I think I want the variable to be  Shared  after all.
Alexandre SimõesManager / Technology SpecialistCommented:

If that's what you want to do just:

Shared _HasBeenMadeVisibleOnce As Boolean = False

It will behave as expected.
Static on VB is a whole different thing as you can read from the link
Tom KnowltonWeb developerAuthor Commented:
Thank you, both.
Tom KnowltonWeb developerAuthor Commented:
>>>Static on VB is a whole different thing as you can read from the link

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.