Need help with Property (get/set) for arrays
Hi everyone :)
I'm manually porting some C++ code to C# and am trying to convert some array properties.
What I'd like (although this semantically incorrect) is something like this:
private double[] m_oWallLoad = new double[10];
public double[] oWallLoad [int index]
{
get{ return ConvertToMetric(m_oWallLoad[index] ,1); }
set{
if (value < 0)
value = 0;
if (value > Definitions.MAXVALUE)
value = Definitions.MAXVALUE;
m_oWallLoad[index] = value;
}
}
I don't think I can use indexers because indexers seem to be general for a particular type, where as I want a specific property for each individual variable (I have dozens similar to this WallLoad).
Can anyone give me a suggestion or two on how to do this?
Cheers and much appreciated :)
Dylan
[926 byte] By [
Dylankreid] at [2007-11-20 11:11:44]

# 1 Re: Need help with Property (get/set) for arrays
...
I don't think I can use indexers because indexers seem to be general for a particular type, where as I want a specific property for each individual variable (I have dozens similar to this WallLoad).
Can anyone give me a suggestion or two on how to do this?
Cheers and much appreciated :)
Dylan
public class Wallloads{
private double[] m_oWallLoad = new double[10];
public double this [int index]{
get{ return m_oWallLoad[index]; }
set{
double val = value;
if (value < 0){
val = 0;
}
if (value > Definitions.MAXVALUE){
val = Definitions.MAXVALUE;
}
m_oWallLoad[index] = val;
}
}
}And thats all about . Now you have Wallloads and can use this as an indexed class instead of your double array. You also can use enumerator for this to go through it using foreach
# 2 Re: Need help with Property (get/set) for arrays
for array'd properties, if you use a collection object, you can just provide a getter. that will allow the object referenced by the property to be changed (add, remove, index of and so on).
if you have an array property, provide the get and set with no indexer, but you'll need to assign any changes back if you want them to be persisted:
// collection:
private List<double> myList = new List<double>();
public IList<double> CollectionProperty { get { return myList; } }
myObject.CollectionProperty[0] = 111;
myObject.CollectionProperty.Add(54.5);
double[] val = myObject.ArrayProperty;
val[0] = 111;
myObject.ArrayProperty = val;
# 3 Re: Need help with Property (get/set) for arrays
Read section 1 of this:
http://www.codeproject.com/csharp/csharptips.asp
pay close attention to the warning
If I were you, i'd just make a boring GetXXX()/SetXXX() method pair
cjard at 2007-11-9 11:38:21 >
