Solved

Static variable?

Posted on 2006-10-27
8
249 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:Tom Knowlton
[X]
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
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:Tom 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom 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:Tom 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:Tom Knowlton
ID: 17822186
Thank you, both.
0
 
LVL 5

Author Comment

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


Agreed!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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