简体   繁体   English

如何在C#asp.net中有条件地创建类的新实例?

[英]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). 一个构造函数,不传递任何内容(创建新记录),而另一个ID则传递任何内容(用于更新)。 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. 我的问题是该对象不会执行if语句。

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. 在当前上下文中不能使用p。 I could copy my code into each condition, it just seems like I shouldn't need to do that. 我可以将代码复制到每个条件中,似乎我不需要这样做。

How can I use p ? 如何使用p

You need to move the declaration of p to above the if statement: 您需要将p的声明移至if语句上方:

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. 如果在局部作用域内声明变量,则在离开该作用域时将无法访问该变量。

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());   
}

JobPosition声明移至条件之外。

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()); 

}

Move p out of the if/else scope into the method scope. 将p从if / else范围移出到方法范围。

JobPosition p = null;

if (Session["PositionID"] == null)
{
    p = new JobPosition();
}
else
{ 
    p = new JobPosition(Convert.ToInt32(Session["PositionID"]));
}

Declare JobPosition outside the if block: 在if块外声明JobPosition:

JobPosition p;
if (Session["PositionID"] == null)
{
    p = new JobPosition();
}
else
{
    p = new JobPosition(Convert.ToInt32(Session["PositionID"]));
} 

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. 严格不需要null

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());    
} 

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM