Reputation: 1914
I have a class with different constructors. One constructor, nothing is passed through (creating a new record), the other an ID is passed through (which is used for an update). I'd like to test for a condition and make a new instance of the class object based on the outcome. My problem is that the object doesn't carry out of the 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 be used in the current context. I could copy my code into each condition, it just seems like I shouldn't need to do that.
How can I use p
?
Upvotes: 0
Views: 1855
Reputation: 1950
I would be more inclined to do the following.
protected void Position()
{
const string positionIdKey = "PositionID";
var positionId = Session[positionIdKey];
var p = positionId == null
? new JobPosition()
: new JobPosition(Convert.ToInt32(positionId));
p.Update(pTitle.text, pMission.text, pDept.text);
Session[positionIdKey] = p.SaveDB();
}
Upvotes: 0
Reputation: 32258
Move the JobPosition
declaration outside the conditional.
Upvotes: 2
Reputation: 31610
protected void Position()
{
JobPosition p; // No need for null;
if (Session["PositionID"] == null)
{
p = new JobPosition();
}
else
{
p = new JobPosition(Convert.ToInt32(Session["PositionID"]));
}
// Don't need to check if p is null again here too
p.positionTitle= pTitle.text;
p.positionMission= pMission.text;
p.positionDepartment= pDept.text;
Session["PositionID"] = Convert.ToString(p.SaveDB());
}
Upvotes: 1
Reputation: 2523
Declare JobPosition outside the if block:
JobPosition p;
if (Session["PositionID"] == null)
{
p = new JobPosition();
}
else
{
p = new JobPosition(Convert.ToInt32(Session["PositionID"]));
}
Upvotes: 1
Reputation: 19842
Maybe I'm misunderstanding your question, but it seems like you want:
protected void Position()
{
JobPosition p;
if (Session["PositionID"] == null)
{
p = new JobPosition();
}
else
{
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());
}
Upvotes: 4
Reputation: 838296
You need to move the declaration of p
to above the if statement:
JobPosition p;
if (Session["PositionID"] == null)
{
p = new JobPosition();
}
else
{
p = new JobPosition(Convert.ToInt32(Session["PositionID"]));
}
If you declare a variable inside a local scope, it is not accessible when you leave that scope.
Upvotes: 8
Reputation: 1342
protected void Position()
{
JobPosition p = null; //declare only once
if (Session["PositionID"] == null)
{
p = new JobPosition();
}
else
{
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());
}
Upvotes: 0
Reputation: 137148
Change your code to:
JobPosition p = null;
if (Session["PositionID"] == null)
{
p = new JobPosition();
}
else
{
p = new JobPosition(Convert.ToInt32(Session["PositionID"]));
}
The null
isn't strictly needed.
Upvotes: 0
Reputation: 17808
Move p out of the if/else scope into the method scope.
JobPosition p = null;
if (Session["PositionID"] == null)
{
p = new JobPosition();
}
else
{
p = new JobPosition(Convert.ToInt32(Session["PositionID"]));
}
Upvotes: 1