passing info from one form to another

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm working in vb.net and have one form that creates a second form. I need
the first form pass info to the second form.

I tried using a shared property in the first form and got this error :
Cannot refer to an instance member of a class from within a shared method or
shared member initializer without an explicit instance of the class.

I changed the shared property to public and got this error :
Reference to a non-shared member requires an object reference.

Does anyone know how I could do this?

Thanks,
Guy
 
in your form2, create a public variable or property(heheh i dont know what
are the differences of these 2)

dim f2 as new form2
f2.variable1 = "my value1"
f2.variable2 = "my value2"
f2.show()
 
Guy,

Are you sure that you do not need a modal form.

dim frm2 as new form2
frm2.MyPublicPropertyThatINeed = "whatever"
frm.showdialog

Cor
 
Guy said:
Hi,

I'm working in vb.net and have one form that creates a second form. I need
the first form pass info to the second form.

I prefer to use the form constructor to send in any information the form may
need.

Dim f2 As New Form2(myData);
I tried using a shared property in the first form and got this error :
Cannot refer to an instance member of a class from within a shared method
or
shared member initializer without an explicit instance of the class.

A shared member can only talk to other shared members directly.
I changed the shared property to public and got this error :
Reference to a non-shared member requires an object reference.

One of the members is still shared.
Does anyone know how I could do this?

You have to pass some data around at some point. Now it doesn't really make
much sense having the variables shared since then they have class scope
rather than instance scope, and I assume you're trying to access instance
data. Thus, if you don't want to use the constructor option above you can
use liu's suggestion, just make sure that none of the variables or methods
setting them are Shared.
 
I do recommend using constructor to pass data from form to form. Just
initalize the new form with the data you need to pass and handle them in the
constructor of the newly launched form. In C#, it looks like this:

MyNewForm newForm=new MyNewForm(Info1,Info2);
newForm.Show();

In MyNewForm:

public MyNewForm(string Info1, string Info2)
{
#assign value of Info1 and Info2 to their respective variable
}

cheers,
yizhe
 
Back
Top