Click here to Skip to main content
15,902,846 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
from1 contains a variable "Admin_id" n i declare it like this

private string admin_id;
public string Admin_id
{
     get { return admin_id; }
     set { admin_id = value; }
} 


it becomes null when i code
form1 f1 = new form1();
why?

i write this in Administration class to access value of form1 variable (admin_id) n to access i code

string d_id = f1.Admin_id;
Posted
Updated 17-Jun-11 3:09am
v3
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 15:26pm    
Where is the declaration of A_TB1?
--SA
Sweety Khan 16-Jun-11 15:28pm    
it is a textbox. i just drop it in my from n use it as A_TB1.text

Admin_id is a public variable on the form1, so whenevr you create a new instance of the form then automatically all the variables are reinitialized
 
Share this answer
 
Comments
Sweety Khan 17-Jun-11 9:24am    
oki but i want its value :(. how to get it?
for Public type variables use class and define like this :-

C#
public static string Admin_id;


uses :

C#
string ID=classname.Admin_id;
 
Share this answer
 
Comments
Sweety Khan 17-Jun-11 12:22pm    
done! thanx a lot from core of my heart.
replace

admin_id. = A_TB1.Text;


with this:
string admin_id = A_TB1.Text;


for further queries comment here!!
 
Share this answer
 
Comments
Sweety Khan 16-Jun-11 21:28pm    
yes i defined it like this so tht i can access it in other classes too
private string admin_id;
public string Admin_id
{
get { return admin_id; }
set { admin_id = value; }
}
Ajvirk 16-Jun-11 21:51pm    
r u getting the value in the database I mean in table u created at databadse?
Sweety Khan 17-Jun-11 9:11am    
@Ajvirk: i edit the question, plz read n then again ask me
Your public property is null because you've never initialised it with a value.

Based on your code, try doing the following:

form1 f1 = new form1();
f1.Admin_id = "someAdminId";
string d_id = f1.Admin_id;


You can see that Admin_id now holds someAdminId.
 
Share this answer
 
Comments
Sweety Khan 17-Jun-11 9:30am    
sorry i didnt mention it in my ques tht Admin_id is initialized like this in form1.
Admin_id = A_TB1.Text;
UJimbo 17-Jun-11 10:55am    
Might I suggest then that you create an event for your A_TB1 textbox On TextChanged where you give the value of your textbox to to the public property like:
private void A_TB1_TextChanged(object sender, EventArgs e)
{
Admin_id = A_TB1.Text;
}

On your Administration class, create the variable like so:
string d_id = string.Empty;

Finally, create a method on your Administration class that does what you tried to do above:

public string UpdateValue()
{
return f1.Admin_id;
}

Call the above method whenever you want to get the latest value from your textbox, like d_id = UpdateValue();

Hope I was helpfull

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900