VB.net 2005 Global Variables and Functions

jaydwest

JayW
Local time
Yesterday, 18:26
Joined
Apr 22, 2003
Messages
340
I'm an experienced Access Developer but new to VB.net. I'm trying to figure our how and if VB.net provides the equivalent to Global Variables and Global Functions.

It looks like I need to use classes but I can't seem to figure out how. I know I'm using the wrong part of my brain but would really like to create function that I can access throughout my Application.

Can someone help this ol' dog learn some new tricks?

Thanks.
 
Public x as string

This is the closest I know of to a global variable.
 
Yes it's time to start learning about classes.

Create a public class like so:


Code:
Public Class MathHelper

Public Sub New()

End Sub


End Class


Now if you want to create a function that you wish to access throughout your app, create a shared function within that class.



Code:
Public Class MathHelper

Public Sub New()

End Sub


Public Shared Function MultipliedByTwo(i as integer) as Integer

return convert.toint32(i*2)

End Function


End Class


and call it anywhere in your code (because both your function and class are declared public) like so:


Code:
Dim x as Integer = MathHelper.MultipliedByTwo(myInt)
 
Does Access VBA support classes then?

I always thought it didn't... and before .NET the only true OO languages were C++ and Java and some others that I forget :confused:
 
Another thing you can do is if you just want global variables, is to create a module and declare your variables public and then import the module into your form.

For Example
Public Module Module1
Public gstrCustCode as String
Public gstrCustName as String
...


and then

Imports Program.Module1

That may not be the best way but that is how I was taught, and it seems to work just fine.
 

Users who are viewing this thread

Back
Top Bottom