Monday 15 September 2014

c# - Adding a bool for each property -



c# - Adding a bool for each property -

i'm building c# class works 2 different info sources. load info source , take configuration set function. want several tasks on properties within object.

for example.

public string streetaddress { { homecoming _streetaddress; } set { if (value.length <= 64) _streetaddress = value; else _streetaddress = value.substring(0, 1024).trim(); } } public string city { { homecoming _city; } set { if (value.length <= 128) _city = value; else _city = value.substring(0, 128).trim(); } } public string state { { homecoming _state; } set { if (value.length <= 128) _state = value; else _state = value.substring(0, 128).trim(); } }

so holds info 1 side. hoping able store , set alter flag on each property. if take state example. if person moved texas illinois want set bool within property note alter able loop on changes before saving object db. don't see way assign state variable within property. best way write object on top of command or there more creative way store multiple strings within 1 property?

if you'd oop way of doing thing, can:

define interface , class holding property, such as:

interface ipropertyslot { bool isdirty { get; } void resetisdirty(); object untypedvalue { get; } } class propertyslot<t>:ipropertyslot { public t value { get; private set; } public bool setvalue(t value) { if (!equals(_value, value)) { value = value; isdirty = true; homecoming true; } homecoming false; } public bool isdirty { get; private set; } public void resetisdirty() { isdirty = false; } public object untypedvalue { { homecoming value; } } }

store properties within class in dictionary string (for name of property) ipropertyslot , get/set them through pair of methods:

void setproperty<t>(string name, t value) { ipropertyslot property; if (!_properties.trygetvalue(name, out property)) { property = new propertyslot<t>(); _properties[name] = property; } ((propertyslot<t>)property) .setvalue(value); } t getproperty<t>(string name) { ipropertyslot property; if (!_properties.trygetvalue(name, out property)) { property = new propertyslot<t>(); _properties[name] = property; } homecoming ((propertyslot<t>)property).value; }

finding changed properties later matter of going on _properties.values , finding of them isdirty.

this approach gives way add together more functionality properties in oo manner (such raising propertychanged/propertychanging events, mapping db fields, etc.).

c# .net properties

No comments:

Post a Comment