14

In my class I have private variables and properties like this.

private string _itemCOde=string.Empty;
private string  _itemName=string.Empty;

public string ItemCode
{
    get { return _itemCode; }
    set { _itemCode = value == null ? value : value.Trim();}
}

public string ItemName
{
    get { return _itemName; }
    set { _itemName = value == null ? value : value.Trim();}
}

According to this properties I create Item objects after selecting the data from the sql table.

Now, if database table altered and add a new column called cost, then I have to add another property to the class. Without adding new properties to the class is there any way do declare properties according to the table fields dynamically.

JHobern
  • 866
  • 1
  • 13
  • 20
Snj
  • 883
  • 3
  • 9
  • 20
  • possible duplicate of [How do I create dynamic properties in C#?](http://stackoverflow.com/questions/947241/how-do-i-create-dynamic-properties-in-c) – nawfal Jul 20 '14 at 06:18

6 Answers6

23

You could use an ExpandoObject:

Represents an object whose members can be dynamically added and removed at run time.

dynamic expando = new ExpandoObject();
expando.Cost= 42.0;
expando.ItemName = "Shoes";
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • In addition, look in the System.Dynamic namespace. there's bases classes like DynamicObject that can help you out. http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx – Justin Largey Jun 01 '11 at 03:50
  • Thanks for the reply. In my Item class I have already declared get set method for the itemCode and item name. Now user can create object by calling the constructor like Public Item(string itemcode,strin itemName). My problem is I'm getting extra property names from the table (table fields). Those are coming as strings("CostPrice", "SellingPrice"). How can convert those strings to properties at the runtime. – Snj Jun 01 '11 at 04:36
3

Maybe what you need is a Dictionary.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
2

If new columns are added to the databases you need to alter your code to specify how new columns should be used.

If you need just to show content of the table despite of the field in the table, then you could use DataTable class.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
2

You can dynamically associate names and values with each other using a Dictionary<TKey,TValue>. You could combine this with your own wrapper using the indexer:

public string this[this key]
{
    get { return dictionary[key]; }
    set { dictionary[key] = value == null ? null : value.Trim(); }
}

Still, if the database changes, then you really should probably change your object because it changed.

pickypg
  • 22,034
  • 5
  • 72
  • 84
2

If you need to keep the class attributes in sync with the SQL table schema... What you need is LINQ to SQL

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
1

Take a look at DynamicObject. You can wrap a System.Data.DataRow in one to get the functionality that I think you are after.

http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx

Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75