ΗαγΣΗΪΟ ΗαΤΞΥν ΗαΡήγν

γΤΗεΟΙ ΗαδΣΞΙ ίΗγαΙ : auto initialise all public fields in a class



C# Programming
06-01-2009, 12:11 AM
Hi all

I’m looking for some assientance on auto initialising all public fields in a class.

I have a class with about 50+ fields. When an instance of the class is created and a value is assigned to the fields, it appends to the existing static value.

MyClass obj = new MyClass();
obj.Example = “THIS_STRING”; // This would therefore return STATIC_BEGINING_OF_STRINGTHIS_STRING which is desired.

My problem is that in obj.allRecords will only contain “STATIC_BEGINING_OF_STRINGTHIS_ STRING” and not “ANOTHER_STATIC_BEGINING_OF_STRING” plus the other 50 fields as they have not been initialised to their initial default value. I dont want to do it manually in the object as the point is that they are in the list as their default value even if unchanged.

Thanks for any help.

class MyClass {

public MyClass() { }

public List allRecords = new List();

private string _example = "STATIC_BEGINING_OF_STRING";
public string Example
{
get { return _example; }
set
{
_example += value;
allRecords.Add(_example);
}
}

// 50 more encapsulated fields

private string _recordEnd = "ANOTHER_STATIC_BEGINING_OF_STRING";
public string RecordEnd
{
get { return _recordEnd; }
set
{
_recordEnd += value;
allRecords.Add(_recordEnd);
}
}
}