Am I doing something wrong? (Variables)

M

MikeY

Hiya all,

I am developing a windows form application. I am coding in C#. What I have
is two forms/Classes, frmMain & frmAdd. In frmMain I have initialize &
instantiated my variable as "public string userName", where I have inputted
a user name. I am trying to get at this data from my frmMain to my frmAdd. I
thought this is the correct syntex for outputting the info in my textbox
(scratching my head), maybe it's not. So please let me know if you can help
or where I'm going wrong. Code is as follows:

namespace Q.A._Testing_Centre
{
public class frmMain : System.Windows.Forms.Form
{
public string userName; <<<---- I want the data from here
....
....
....


namespace Q.A._Testing_Centre
{
public class frmAdd : System.Windows.Forms.Form
{
private void SubMainMenu()
{
frmAdd Main = new frmMain();
txtBoxSample.Text = Convert.ToString(Main.userName);<<<--- to output in my
textbox here
....
....
....


Or maybe if someone has a better alternative, please feel free to let me
know. All help is truly appreciated & thank you all in advance.

MikeY
 
H

Hector Martinez

Take a look to some fix....


namespace Q.A._Testing_Centre
{
public class frmMain : System.Windows.Forms.Form
{
public static string userName;
//public string userName;

namespace Q.A._Testing_Centre
{
public class frmAdd : System.Windows.Forms.Form
{
private void SubMainMenu()
{
//you don't need this..
// frmMain Main = new frmMain();

//just do this...
txtBoxSample.Text = Convert.ToStrin(frmMain.userName);
}
}
}

You can have access to the frmMain userName field in
that way due to this field is set to be "static", and you
can have access to it wherever you want...

I hope this help.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top