tags:

views:

75

answers:

4

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 = argUnit;
    this.Discount = argDiscount;
    this.WhetherInPercent = argWhetherInPercent;
    this.CategoryID = argCategoryID;
}
#endregion
+2  A: 

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

public int CategoryId { get; set; }
A_Nablsi
Nothing to say about the constructors.
A_Nablsi
How to improve the Constructor Code?
RPK
I prefer `public ItemMaster(string ItemName, ...) { this.ItemName = ItemName; ... }`. No need to prepend 'arg', `this.` makes the difference. But there's not much more to say about it without actually knowing the design, it smells a bit of too many related parameters, but that doesn't mean much by itself.
Vinko Vrsalovic
Vinko Vrsalovic, thanks for the editing it is much more better, sorry my English is good but not all the times depends on the stress. Thanks again :)
A_Nablsi
+2  A: 

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; }
Lorenzo
@Lorenzo: There might not be anything wrong but just wanted to know how to give professional touch to it.
RPK
@Lorenzo: I prefer using the automatic method. it's cleaner and faster to write. IMO, keep things simple as much as you can.
Kamyar
@Kamyar: It's just a matter of preference. In some case the automatic properties cannot be used at all.
Lorenzo
@RPK: I just wrote that "on premise". As there are no error, and there's no better way to write that code, the only things you can do is to beautify your code. Almost all the people that gave an answer in fact, told you the same things...
Lorenzo
A: 

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.

Kamyar
A: 

Why do you want initialize all your classses data at first time, just call constructor to create object with no argument and initialize data when you want

public ItemMaster() : this("")
{
}

public ItemMaster(string argItemName) : this(argItemName, "")
{
  this.ItemName = argItemName;  
}

public ItemMaster(string argItemName, string argItemCode) : this (argItemName, argItemCode, 0)
{
....
}
.....
public ItemMaster(string argItemName, string argItemCode, decimal? argStockInHand, decimal? argAlertLevelQty, string argUnit, decimal? argDiscount, string argWhetherInPercent, int argCategoryID)
{
....
}

and just where call the main constructor when you have all parameter initialized, I prefer to create class and when needed pass its parameters.

Also use

public propertytype propertyname {get;set;}
SaeedAlg