Declaring and Passing value to public variable
-
hi, I'm having a problem in declaring and passing a value to a public variable in C#.Net. I am a VB.Net user not a C#.Net user, i just thinking if i convert my VB.Net code to C#.Net and that's what I'm doing now. In VB.Net you can create a Module where you can declare all your public variables to use it again and again. My problem now is, how can i declare a public variable/s in C#.Net so that i can use it for many times and also to pass a value to that variable so that i can get the value i pass to public variable. Example.
Public strFullName as String
, wherestrFullName
is the variable where i pass the FullName of the person who logged to the system. Thank You.Don't block the drive way of all the newbies in programming. :)
-
hi, I'm having a problem in declaring and passing a value to a public variable in C#.Net. I am a VB.Net user not a C#.Net user, i just thinking if i convert my VB.Net code to C#.Net and that's what I'm doing now. In VB.Net you can create a Module where you can declare all your public variables to use it again and again. My problem now is, how can i declare a public variable/s in C#.Net so that i can use it for many times and also to pass a value to that variable so that i can get the value i pass to public variable. Example.
Public strFullName as String
, wherestrFullName
is the variable where i pass the FullName of the person who logged to the system. Thank You.Don't block the drive way of all the newbies in programming. :)
Google not working for you?
class YourClass
{
public string fullName;
// other stuff goes here
}Having said that, using public variables is a Bad Thing. You should use properties instead.
class YourClass
{
private string _fullName;
public string FullName
{
get { return _fullName; }
set { _fullName = value; }
}
}Cheers, Vikram.
"I will put my new found knolage to good use" - Captain See Sharp. "Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.
-
Google not working for you?
class YourClass
{
public string fullName;
// other stuff goes here
}Having said that, using public variables is a Bad Thing. You should use properties instead.
class YourClass
{
private string _fullName;
public string FullName
{
get { return _fullName; }
set { _fullName = value; }
}
}Cheers, Vikram.
"I will put my new found knolage to good use" - Captain See Sharp. "Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.
what's wrong with this? code of Class
class getUserName { private String _UserName; public String UserName { get { return this._UserName; } set { this._UserName = value; } } }
code of Button 1 in Form 1 i put the "myUsername" to Username inside the getUsername class so i can get the Username whereever i open a different form.private void button1_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); objgetUsername.UserName = "myUsername"; }
code of Button 2 in Form 2 here i will retrieve the myUsername to put it in the Messagebox to show the Username i used.private void button2_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); MessageBox.Show(getUsername.UserName); }
my problem now is, I'm getting a null value in Form 2 Button 2 everytime i click the Button 2. Any help out there. Thank You.if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }
-
what's wrong with this? code of Class
class getUserName { private String _UserName; public String UserName { get { return this._UserName; } set { this._UserName = value; } } }
code of Button 1 in Form 1 i put the "myUsername" to Username inside the getUsername class so i can get the Username whereever i open a different form.private void button1_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); objgetUsername.UserName = "myUsername"; }
code of Button 2 in Form 2 here i will retrieve the myUsername to put it in the Messagebox to show the Username i used.private void button2_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); MessageBox.Show(getUsername.UserName); }
my problem now is, I'm getting a null value in Form 2 Button 2 everytime i click the Button 2. Any help out there. Thank You.if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }
Simple - the two objects you create in the two methods are different. Anyway, this is wrong on so many levels. Your classes should be nouns, not verbs. Also, you don't even need a new class to store the user name in this case. In your form, just declare
private string _userName;
Then, in your methods, you can read and modify_userName
directly.Cheers, Vikram.
"I will put my new found knolage to good use" - Captain See Sharp. "Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.