Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String variable value become null

Status
Not open for further replies.

laic

IS-IT--Management
Jul 24, 2003
17
US
The problem that I am having is that I declare a private string variable, and use it inside a method, supposely the value will be set inside this method. And on other method the value will be retrieve back and display. After tracing the code in debug mode, I saw the value change in method A, but when it comes to display, the value changes to null again.

//code below

private string display = "";
private Label label1 = new Label();

private void Method1()
{
display = "Hello!!"; //This has assigned to the string
//value successfully.
}

private void Method2()
{
Label1.Text = display; //I have traced this, and here it
//showed as NULL;
}

I don't understand why this is happening??
Isn't private string display supposed to be a global variable?

Can someone please explain this to me??
I am confused. . . .

Laic
 
I have figured out, I left out the static word in the declaration part.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top