How do I conditionally create a new instance of a class in C# asp.net? -


i have class different constructors. 1 constructor, nothing passed through (creating new record), other id passed through (which used update). i'd test condition , make new instance of class object based on outcome. problem object doesn't carry out of if statement.

protected void position() {     if (session["positionid"] == null)     {         jobposition p = new jobposition();     }     else     {          jobposition p = new jobposition(convert.toint32(session["positionid"]));     }     p.positiontitle= ptitle.text;     p.positionmission= pmission.text;     p.positiondepartment= pdept.text;     session["positionid"] = convert.tostring(p.savedb());    } 

p cannot used in current context. copy code each condition, seems shouldn't need that.

how can use p?

you need move declaration of p above if statement:

jobposition p;  if (session["positionid"] == null) {     p = new jobposition(); } else {      p = new jobposition(convert.toint32(session["positionid"])); } 

if declare variable inside local scope, not accessible when leave scope.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -