开发者

Better way to write this Properties and Constructor code?

I am using Visual Studio 2008 (C#). Is there any better way to write this code? I am a poor programmer.

#region Properties
public string ItemCode
{
    get { return _itemCode; }
    set { _itemCode = value; }
}

public string ItemName
{
    get { return _itemName; }
    set { _itemName = value; }
}

public decimal? StockInHand
{
    get { return _stockInHand; }
    set { _stockInHand = value; }
}

public decimal? AlertLevelQty
{
    get { return _alertLevelQty; }
    set { _alertLevelQty = value; }
}

public string Unit
{
    get { return _unit; }
    set { _unit = value; }
}

public decimal? Discount
{
    get { return _discount; }
    set { _discount = value; }
}

public string WhetherInPercent
{
    get { return _whetherInPercent; }
    set { _whetherInPercent = value; }
}

public int CategoryID
{
    get { return _categoryID; }
    set { _categoryID = value; }
}
#endregion

#region Constructors
public ItemMaster()
{  }

public ItemMaster(string argItemName, string argItemCode, decimal? argStockInHand, decimal? argAlertLevelQty, string argUnit, decimal? argDiscount, string argWhetherInPercent, int argCategoryID)
{
    this.ItemName = argItemName;
    this.ItemCode = argItemCode;
    this.StockInHand = argStockInHand;
    this.AlertLevelQty = argAlertLevelQty;
    this.Unit = argUn开发者_JAVA技巧it;
    this.Discount = argDiscount;
    this.WhetherInPercent = argWhetherInPercent;
    this.CategoryID = argCategoryID;
}
#endregion


Nothing is wrong with your code.

I am a lazy programmer so I often use automatic properties in my code.

public string Unit { get; set; }


All of the properties in your example can be written with 'automatic properties', for example:

public int CategoryId { get; set; }


If you don't need to modify your properties int your get/set, Go with `public string ItemCode {get; set;}
But if you have to change the values, You have to define a private var for it and define get/set as you did in your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜