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õesConnect With a Mentor Manager / 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.

ZeonFlashConnect With a Mentor Commented:
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!!  


Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

All Courses

From novice to tech pro — start learning today.