passing vars through forms

  • Thread starter Thread starter Daniel Sélen Secches
  • Start date Start date
D

Daniel Sélen Secches

Hi all....

I have a frmMain and a form2.

form2 has a var named conn for exemple....
public conn as string
from frmMain i can do this...

dim f as new form2
f.conn = "blblbl"
f.showdialog

but i want to do the opposite, from the form2 access the frmMain....

i've tried to do like this..

frmMain:
public connstring as string....

and from form2 use that Connstring to open a connection
form2:
cmd.connection = frmMain.connstring

but i couldn't.

I'm trying to do it to centralize the conn string of my application, so if i
have to change the connection i'd change in only one place.

Tks....
 
Daniel,

First of all, try to avoid using public variables anywhere in your
application as it is not good object orientated practise to do so. If you
need to provide public access to values in another form or class etc. then
do it using public properties instead. If you want to pass a connection
string to your second form, a couple of ways to achieve this are as
follows:-

In both cases, declare a module level variable in Form2...

Private mstrConnectionString as String

Then do one (or both) of the following...

1) Create an overloaded 'New' method in Form2 and pass the connection string
to it when creating an instance of that form...

Public Sub New(ByVal strConnectionString as String)
mstrConnectionString = strConnectionString
End Sub

...then when you declare an instance of Form2 in Form1, pass the
connection string...

Dim objForm2 as New Form2(myConnectionString)

objForm2.Show()

2) Create an overloaded 'Show' method in Form2 and pass the connection
string to it when showing the form...

Public Overloads Sub Show(ByVal strConnectionString as String)
mstrConnectionString = strConnectionString
End Sub

...then when you show Form2 from Form1, pass your connection string to
the overloaded Show method...

Dim objForm2 as New Form2()

objForm2.Show(myConnectionString)

In Form2 you now have a private variable (mstrConnectionString) containing
the connection string which you can use anywhere within the scope of that
form instance.

I hope this gives you a few pointers to work on.

Gary
 
Hello,

In form1 generale declaration

Public CancelPressed As Integer



use form1 variable into form 2


If form1.CancelPressed = 1 Then
Call btnCancel_Click
Exit Sub
End If


Thanks,


Warm Regards,

Ayaz Ahmed
Software Engineer & Web Developer
Creative Chaos (Pvt.) Ltd.
"Managing Your Digital Risk"
http://www.csquareonline.com
Karachi, Pakistan
Mobile +92 300 2280950
Office +92 21 455 2414
 
Back
Top