VB.net 2005 Global Variables and Functions (1 Viewer)

jaydwest

JayW
Local time
Today, 12:48
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.
 

AxGryndr

Registered User.
Local time
Today, 11:48
Joined
Oct 13, 2008
Messages
22
Public x as string

This is the closest I know of to a global variable.
 

dan-cat

Registered User.
Local time
Today, 19:48
Joined
Jun 2, 2002
Messages
3,433
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)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 19:48
Joined
Jul 9, 2003
Messages
13,416
When I first ventured into VB.Net I found it very easy, the visual basic language was very similar to the VBA language I was using in MS Access. However I was puzzled there was code in examples by other people that I didn't understand, the code contained classes, interfaces and other strange things I was not familiar with.

I started trying to understand these objects, however I found it difficult to learning a new way of programming in a new environment. I realized I would be better off learning classes in a familiar environment environment namely MS Access!

That's what I am doing now, so if you are interested in learning class modules in MS Access, then I would be willing to collaborate.
For more information see this thread here:
 

SQL_Hell

SQL Server DBA
Local time
Today, 19:48
Joined
Dec 4, 2003
Messages
1,347
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:
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 19:48
Joined
Jul 9, 2003
Messages
13,416
>>>Does Access VBA support classes then?<<<

Well yes and no, they are probably not classes in the way you understand them, in MS Access you can have "class modules" they work in a similar way two the classes you mention, however they do have limitations.

My point is it's far easier to learn new techniques in a familiar environment, and the basic techniques are the same. Once I have grasped these techniques then I will be ready to test them out in the VB.Net environment. Once I've done this I will be more informed, more able to comment with authority.
 

devilsphere

New member
Local time
Today, 14:48
Joined
Oct 23, 2008
Messages
4
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

Top Bottom