Solved

Static variable?

Posted on 2006-10-27
8
246 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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