global data in .net

  • Thread starter Thread starter CsProviders
  • Start date Start date
C

CsProviders

i am a newbie to .net programming, i am having problem
with global data and form instanciation. My questions are
How do i maintain global data in windows forms
applications? How do i navigate from one form to another
form class? (example me.hide() and form2.show)
TIA
CSP
 
CsProviders said:
i am a newbie to .net programming, i am having problem
with global data and form instanciation. My questions are
How do i maintain global data in windows forms
applications?

Shared members of classes (AKA fields in Modules) live from their first
usage til the application's end.
How do i navigate from one form to another
form class? (example me.hide() and form2.show)

Write your own Sub Main:

Sub Main
Dim F as Form1
f.Show
Application.Run
End Sub

In Form1:
Dim f2 as Form2
f2 = New Form2

Me.Hide
f2.Show
 
In VB6, you were able to refer to any form by the form-name (like Form1,
frmMain, etc) and you were also able to create multiple instances of any
form like:
Dim frm as New Form2

In VB.NET you cannot code against the form-name (the class) anymore. You
always have to create an instance of any form before using it. The new
problem here is that you cannot get a hold on any of the application's forms
(instances) unless you have a public variable referencing that form for you
to use (global data as you said).
One way of doing that is adding a Module to your app and declaring variables
to hold references to the form instances that you'll need across your app:

Module YourModule
Public mainForm as frmMain
Public clockForm as frmClock

End Module

In your code, when you open the main form for the first time, you store the
reference in the appropriate variable:

mainForm = New frmMain()
mainForm.Show()

Then you can use the variable mainForm anywhere else in your app to
manipulate the main form.

That's the idea.

HTH
 
Actually i did try both solutios, but unable to hide the
form1. And i got into a problem of showing two form at the
same time. So i used visible property of form1 as false to
get rid of its visibility. I think i m missing something
here...anyways i have another question. If i have nearly
40 to 50 forms in an application..hiding all these form
not going to create any problem in the application, right?
TIA
CSP
 
Back
Top