user4158272
user4158272

Reputation:

Cannot access variable value of Parameterised constructor variable value

I have a parameterized constructor like below.

public abc(string c)
{
   a=c;
}

Then i have Button Event Handler like below.

private void btnConnect_Click(object sender, EventArgs e)
{
   MessageBox.Show(c);
}

So when i do this , When Message box appears it shows nothing it was blank. What is the error ? I have debugged the code i found that constructor has value but message box is not getting value it is null.

Upvotes: 0

Views: 40

Answers (1)

Nagahornbill
Nagahornbill

Reputation: 121

As per the provided code in the question, i think you are trying to access local variable 'c' which has scope just for the constructor, outside the constructor. You can show variable 'a', which is a field of class 'abc' having scope for the class in the MessageBox.Show() method to get the same result. If you have a different declaration of c which you are trying to access in Button click event, make sure its been initialized properly.

Upvotes: 1

Related Questions