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 and they both have the same default
namespace "Q.A._Testing_Centre". In frmMain I have initialize & instantiated
my variable as "public string userName", where the user input name is
stored. I am trying to get the stored data propertied 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()
{
frmMain Main = new frmMain();
txtBoxSample.Text = Convert.ToString(Main.userName);<<<--- to output in my
textbox here
....
....
....


I have tried a sample textbox with in frmMain and this data is in fact
stored until I exit my program. But I cannot seem to reproduce the same
textbox output in frmAdd. I seem to get a blank textbox area. Alternatively
I have try "txtBoxSample.Text = Main.userName" syntex which does not work as
well.
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
 
I

Ian Cooper

Hi Mike,
I am not clear from your post exactly what error you are receiving. One question from your code
frmMain Main = new frmMain();
txtBoxSample.Text = Convert.ToString(Main.userName);<<<--- to output in my

where is the userName field being initialized? Is it being set in the constructor for frmMain. If not I think you are using userName before you initialize it.
I am also not sure why you are using COnvert.ToString, when userName is already of type string.
Finally you would be better making userName private and exposing it through a public property, i.e.:
private string userName
public string UserName
{
get { return username ; }
set {username = value ;}
}
as it gives you some future proofing if you want to change your implementation of this property.
Ian Cooper
wwww.dnug.org.uk
 
M

MikeY

Hi Ian,

There is no error. But what I am getting is a blank textbox, instead of it
being populated with the user name. I am doing this test for myself because
I'm still learning C#. What I wanted to do was hold the user's name in
userName upon login. Then when they update the database their name would
automatically be added to the db without them being given the chance of
altering or changing their name. I was just trying convert.Tostring because
I was trying all my options on why this syntex was not working. I have also
tried "txtBoxSample.Text = Main.userName;" without success.

But I will try using the get/set method. But this still puzzles me. It
should work. Hmmmmmmmmmmm I'll keep pluggin away with it.
Thanks anyways Ian.

MikeY

Ian Cooper said:
Hi Mike,
I am not clear from your post exactly what error you are receiving. One question from your code my

where is the userName field being initialized? Is it being set in the
constructor for frmMain. If not I think you are using userName before you
initialize it.
I am also not sure why you are using COnvert.ToString, when userName is already of type string.
Finally you would be better making userName private and exposing it
through a public property, i.e.:
private string userName
public string UserName
{
get { return username ; }
set {username = value ;}
}
as it gives you some future proofing if you want to change your
implementation of this property.
 
I

Ian Cooper

But what I am getting is a blank textbox, instead of it being populated with the user name.
What I'm still not clear on is where you are initializing the userName string. It will be blank when constructed, so when you assign the control's Text to the string it looks to me like it is null.

Ian Cooper
wwww.dnug.org.uk
 
M

MikeY

Hi Ian,

I am initializing the userName string with in the frmMain.Password method.
Below is a bigger sample of my frmMain

namespace Q.A._Testing_Centre
{
public class frmMain : System.Windows.Forms.Form
{
public string userName; <<------Here
private string userTitle;

public frmMain()
{
InitializeComponent();
mainSetUp();
LoginSetUp();
}
....
....
....
private void PasswordEnter(object sender, System.EventArgs e)
{
try
{
int userinput = int.Parse(txtBoxPassword.Text);
sqlConnection1.Open();
string select = "SELECT * FROM Employee WHERE (EmpID = '" + userinput +
"')";
SqlDataAdapter daPassword = new SqlDataAdapter(select, sqlConnection1);
DataSet dsPassword = new DataSet();
daPassword.Fill(dsPassword, "Employee");
SqlCommand cmd = new SqlCommand(select, sqlConnection1);
SqlDataReader myReader = cmd.ExecuteReader();
while(myReader.Read())
{
userName = myReader.GetString(1) + " " + myReader.GetString(2); <<------
Here
userTitle = myReader.GetString(3);
}
if(userName.ToString() != "")
{
userValidation();
}
}
catch
{
MessageBox.Show("Please Enter A Valid Password");
txtBoxPassword.Text = "";
txtBoxPassword.Focus();
}
finally
{
sqlConnection1.Close();
}
}
}
....
....
....

But I am looking at get/set accessor today as you have suggested earlier.
Maybe this will be the better way of doing this accessing. Yes I agree that
it is blank, but after login the user name is grabbed from the db and
inserted into my string userName. Then from my sencond form frmAdd is where
I try to output to a textbox. Just in case it is still visible in the
textbox with in frmMain. If I read correctly, the namespace would have no
bearing on this trouble.

Thank again Ian.

MikeY

Ian Cooper said:
with the user name.
What I'm still not clear on is where you are initializing the userName
string. It will be blank when constructed, so when you assign the control's
Text to the string it looks to me like it is null.
 

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