Solved

Static variable?

Posted on 2006-10-27
8
243 Views
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?
0
Comment
Question by:knowlton
  • 5
  • 2
8 Comments
 
LVL 17

Assisted Solution

by:ZeonFlash
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()
            'Valid
            Static _HasBeenMadeVisibleOnce As Boolean = False
      End Sub

End Class
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 300 total points
ID: 17822048
Hi...

Take a look: http://msdn2.microsoft.com/en-us/library/z2cty7t8.aspx

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.

Alex
0
 
LVL 5

Author Comment

by:knowlton
ID: 17822083
Okay...got 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
          SearchEquipment()
          _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!!  


Thanks,

Tom
0
 
LVL 5

Author Comment

by:knowlton
ID: 17822099
Alex:

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  )
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 5

Author Comment

by:knowlton
ID: 17822178
Ummm....I think I want the variable to be  Shared  after all.
0
 
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
0
 
LVL 5

Author Comment

by:knowlton
ID: 17822186
Thank you, both.
0
 
LVL 5

Author Comment

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


Agreed!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now