Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where do I declare a global variable?

Status
Not open for further replies.

JABOSL

Programmer
Jan 25, 2006
35
Where do I declare a global variable? I need it to be seen in both classes. I thought it was after imports... and before Public Class Form1 but I can't get any syntax to work there.


Imports System.Threading


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Class


Public Class SessionClass

End Class
 
This will make the variable global only in this class. The only way i know how to make a variable global between classes it so create/set the variable in a property in one class and then call it from another, but when you create the instance from class2 back to class1, you might be resetting the value of the variable to its default value.

I have provided code that will create a global variable with in a class

Code:
imports system
public class1
 Inherits System.Windows.Forms.Form

 dim text as string

 public sub Form1_Load(sender as object, e as eventargs)
  call FirstSub()
  call SecondSub() 
 end Sub

 public sub FirstSub
  text = "new text"
 end sub

 public sub SecondSub
  Console.WriteLine(text) 'Output will be "new text"
 End Sub
End Class
 
One way is to create a new project, say call it prjGlobal.
Add a new class to this project, and declare any variables as Public Shared. Then add a reference to this new project from any projects you wish to utilise these global variables, and add an import to this new class in prjGlobal, either through code, or in the Imports settings of the project(s) you wish to utilise your global variables.

Sweep
...if it works dont f*** with it
curse.gif
 
Ok, so if I declare X like this:
Public Class Form1
Inherits System.Windows.Forms.Form
Public X As Long

It's a public variable of the class Form1. I should be able to access it from another class by saying Form1.X = Form1.X + 1 right? Apparently not. What am I missing?
 
You should in fact add a module to your project and declare any global variables there, which are then accessible in the entire project.

Using global variables like these is frowned upon though, and there's usually a better way. What exactly will be the use of that variable?

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
I need to keep track of the number of threads currently running in my program. I was going to have the main thread increment X and each thread it starts (they just die when they're done) will have to decrement X before they end.

This question came about as a way to do that. But it will not work anyway as these threads will be stepping all over eachother trying to decrement X. So it would seem I'll have to make a public sub in Form1 with the code

Synclock(x_lock)
X = X - 1
End Synclock

to prevent this.

However I still want to understand why other classes can't see a public variable of Form1 with code like Form1.X = ....

And now I also want to know how to declare a variable in a module?
 
1)

Code:
Public Module Module1
  Public X As Integer
End Module

From every class in your project you can now access this X. It is of course wise to use another more specific name for a variable like this, since you are likely to use X somewhere else as well.

2)

You cannot use Form1.X because you are not referencing an instance of the form.

To demonstrate this:

Code:
Public Class Form1
  Inherits System.Windows.Forms.Form

  Public X As Integer

  Public Sub Form1_Load(bla bla bla) Handles MyBase.Load
    'This will (of course) work:
    Me.X = 1
    'This will not work: (hover your mouse over it to see why in your code window)
    Form1.X = 1
    'This will also work:
    Dim F1 As New Form1
    F1.X = 1 
  End Sub
End Class

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Just on a sidenote:

If you use multiple forms/classes in a project, which need to reference each other regurarly, it is advisable to start the entire from a Sub Main in a module:

Code:
'Remember to set the startup object of your project to Sub Main
Public Module mdlStartup
  Friend F1 As Form1

  Public Sub Main()
    F1 = New Form1
    F1.ShowDialog()
    End
  End Sub
End Module

This way, you can access the methods, fields and properties of Form1 through the use of F1. This is a trivial example of course, but it has many uses! (You can alter/overload the Sub New of the form to set default properties for example)

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
I like trivial examples. They don't hide what they're showing under masses of irrelevant code. X is like that. It stands out as the quintessential variable in an example. X is what I told you. m_threadCnt is what I was using in the code.

I guess I still don't understand why Form1.X = 1 doesn't work. (reference to a non-shared member requires an object reference) gobble gook to me.

Form1 is a class, right? If I'm using form1 I would think I'm using an instance of it. Why do they let me use a class but not let it work the same as an instance of the class?
 
That's what Object Oriented (OO) programming is about. Everything is based on the Object type and most everything needs to be instantiated before you can use it.

About Form1.X = 1 not working, I quote from the VB Docs:

The Me keyword acts as an object variable referring to the current instance. If a procedure is not shared, it can use the Me keyword to obtain a pointer to the current instance.

That's why Me.X = 1 will work, but Form1.X will not (not an instance of the class, but rather the class definition)

If you declare X to be shared it will work however:

Code:
Public Class Form1
  Inherits System.Windows.Forms.Form

  Public Shared X As Integer

  Public Sub Form1_Load(bla bla bla) Handles MyBase.Load
    'This will now work:
    Form1.X = 1
  End Sub
End Class

I'm sure this all very unclear, but it's one of the principles of VB .NET which you must try to understand and take for granted I suppose.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Well maybe not very unclear, just unclear, or maybe still very unclear. I get it now Form1 is the class. If you made an instance of it that instance wouldn't be known as Form1, it might be say F1 since you instigated it as "dim f1 as new form1" or "f1 = new form1" or something. Funny they still even let me use form1. And me is just confusing. Now throw in that I'm also been working in another OO language which of course is slightly different and I get all kinds of weird things in my mind. I guess sometimes things are just the way they are because they are. I'm a recovering VB4 addict and everything started with form1. I can't quite stay on the .net wagon.
 
Yup, that's why I'm doing .net now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top