Barrett Kuethen
Barrett Kuethen

Reputation: 1914

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

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

Answers (9)

Gary Brunton
Gary Brunton

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

George Johnston
George Johnston

Reputation: 32258

Move the JobPosition declaration outside the conditional.

Upvotes: 2

Ta01
Ta01

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

Arun
Arun

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

CodingGorilla
CodingGorilla

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

Mark Byers
Mark Byers

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

Anas Karkoukli
Anas Karkoukli

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

ChrisF
ChrisF

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

asawyer
asawyer

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

Related Questions