How can I declare Global and Public variables like in VB6 in VB.Net ?
Printable View
How can I declare Global and Public variables like in VB6 in VB.Net ?
The same way you do in VB6.
If you want a variable to be available everywhere in the application without having to refer to a class, declare it in a module (using either Public or Global).
Often it makes sense to declare it in a class (such as your main form), in which case declare it there (using Public).
Note however that while it is sometimes useful to have variables available everywhere, there tend to be better alternatives in most cases, but as the list is long it is best to ask about specific situations rather than us try to list them all.
As an example of what si is talking about, many beginners will tend to use a global variable so that they can set it in one form and get it in another. That will get the job done but it's not best practice. If you want to get data from one for to another then the one that knows about the other should either push data to the other one or pull data from the other one.
It might be a good idea for you to follow the Blog link in my signature below and check out my three-part post on Data Among Multiple Forms. It starts with global variables and works up to the proper way to do it.
Hi JMC and sorry ahmed,
if I jump with a question into this thread, but it might belong here.
I haven't got around to converting some VB6 Code to .Net.
the VB6 code (see at the end) I use starts in a MDI Form
is executes 3 Forms:
a) start the Search (Form for entering criteria)
b) return the results from that criteria (Form with Listview), and allow the user to pick one
c) finally open the last Form with the selected item from the Listview
VB6 Code
would you work the same way in .Net to interact with 3 Forms to get the final result?Code:Private Sub mnuSeSuchen_Click()
Dim frmSearch As ServiceSuche 'Form for search criteria
Dim frmList As ServiceListe 'Form results of the criteria entered
Dim frmKunde As ServiceEdit 'finally the one i picked from the Form -Serviceliste-
Dim objclsKunden As clsServiceDisplayn 'Class for the results returned
Dim objclsKunde As clsService ' class for
Set frmSearch = New ServiceSuche
With frmSearch
'show the criteria Form
.Show vbModal
If .OK Then
Set frmList = New ServiceListe
Set objclsKunden = New clsServiceDisplayn
'execute the Class for search
objclsKunden.Load .ResultBANr, .ResultBAVom, .ResultRepNr, .ResultRepVom, .ResultMaNr, .ResultFirma, .ResultPLZ
With frmList
'show the results
.Component objclsKunden
.Show vbModal
If .SE_LfdNr > 0 Then
'select one from the SearchList
Set frmKunde = New ServiceEdit
Set objclsKunde = New clsService
objclsKunde.Load .SE_LfdNr
frmKunde.Component objclsKunde
'open the one I picked
frmKunde.Show
Set objclsKunde = Nothing
End If
End With
'close the Form frmList
Unload frmList
Set frmList = Nothing
Set objclsKunde = Nothing
End If
End With
'close the form frmSearch
Unload frmSearch
Set frmSearch = Nothing
End Sub
regards
Chris
I've no particular situation, the reason of my question is that I stopped learning programming with QBasic, I want to learn modern tools to show others my programs, I tried to learn VB6 as a modern tool instead of QBasic but in the end I decide to learn more modern one, VB 2010 is work fine on my PC, I've old background in programming in DOS, it's not so simple to me to be familiar with the new visual programming, programs I know in QBasic are using public variables, it's common something to have public variables, I thought it's simple in VB.Net, so that I asked, I want to have all old features I know to start practice in VB.Net.