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
Solved

Static variable?

Posted on 2006-10-27
8
248 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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