添加可选参数

时间:2016-02-29 13:07:20

标签: c#

我想在下面的代码中给objTest.TestNumber提供可选参数值。我的代码如下: -

 public class TestInfo
{
    public int Id { get; set; }
    public string TestNumber { get; set; }
}       



public string TestUpdate(TestInfo objTest)
    {
        int retVal = 0;
        try
        {
            dataContext = new AccretiveAPIContext(GetConnection(locationId));
            retVal = dataContext.Database.ExecuteSqlCommand(
                     "EXEC usp_TestUpdate @ID, @TestNumber",
                     new SqlParameter("@ID", objTest.Id),
                     new SqlParameter("@TestNumber", objTest.TestNumber),

                 );
        }
        catch (Exception)
        {
            throw;
        }
        return retVal.ToString();
    }

请告诉我需要做出的更改“@TestNumber”,objTest.TestNumber作为可选参数。我想将其默认值设置为零(“0”)。

请帮忙。

2 个答案:

答案 0 :(得分:6)

甚至更容易 -

            retVal = dataContext.Database.ExecuteSqlCommand(
                 "EXEC usp_TestUpdate @ID, @TestNumber",
                 new SqlParameter("@ID", objTest.Id),
                 new SqlParameter("@TestNumber", string.IsNullOrEmpty(objTest.TestNumber) ? "0" : objTest.TestNumber),

答案 1 :(得分:4)

按如下方式更改TestInfo类:

override func setHighlighted(highlighted: Bool, animated: Bool) {
    if(highlighted){
        viewContent.view.backgroundColor = Constants.Colors.selectedBackground
        for item in self.subviews {
            if ((item as? UIButton) != nil) {
                item.superview?.backgroundColor = Constants.Colors.selectedBackground
            }
        }

    } else {
        viewContent.view.backgroundColor = Constants.Colors.normalCellBackground
        for item in self.subviews {
            if ((item as? UIButton) != nil) {
                item.superview?.backgroundColor = Constants.Colors.normalCellBackground
            }
        }

    }
}

你也可以修改你的方法并说出如下内容:

public class TestInfo
{
    private string _testNumber;

    public int Id { get; set; }
    public string TestNumber
    {
        get
        {
            if (string.IsNullOrEmpty(_testNumber))
            {
                _testNumber = "0";
            }
            return _testNumber;
        }
        set { _testNumber = value; }
    }
}