Static variable?

Posted on 2006-10-27
Last Modified: 2010-04-23
'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?
Question by:Tom Knowlton
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
LVL 17

Assisted Solution

ZeonFlash earned 200 total points
ID: 17821999
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
LVL 30

Accepted Solution

Alexandre Simões earned 300 total points
ID: 17822048

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.


Author Comment

by:Tom Knowlton
ID: 17822083 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!!  


Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now


Author Comment

by:Tom Knowlton
ID: 17822099

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  )

Author Comment

by:Tom Knowlton
ID: 17822178
Ummm....I think I want the variable to be  Shared  after all.
LVL 30

Expert Comment

by:Alexandre Simões
ID: 17822185

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

Author Comment

by:Tom Knowlton
ID: 17822186
Thank you, both.

Author Comment

by:Tom Knowlton
ID: 17822208
>>>Static on VB is a whole different thing as you can read from the link


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question