I have a list of values which i am maintaining like this,
public enum DisplayUnits
{
Vertical = 0,
Horizontal = 1,
Track = 2,
Empty = 3,
}
public static string DisplayUnitsImage(DisplayUnits unit)
{
switch (unit)
{
case DisplayUnits.Vertical:
return @"/image1";
case DisplayUnits.Horizontal:
return @"/image2";
case DisplayUnits.Track:
return @"/image3";
case DisplayUnits.Empty:
return @"/image4";
default:
return @"/image5";
}
}
public static string DisplayUnitID(DisplayUnits unit)
{
switch (unit)
{
case DisplayUnits.Vertical:
return @"1234";
case DisplayUnits.Horizontal:
return @"1235";
case DisplayUnits.Track:
return @"1236";
case DisplayUnits.Empty:
return @"1237";
default:
return @"1238";
}
}
For retrieving the image path i ll call the method by passing the enum.
Can it be written in simpler manner because for adding one value i need to change in three places?
Using of Tuple is recommended in this context?
Let me change my answer completely.
As soon as you state, that you need to hardcode all possible
unitids andimages, you may have them stored into an array in your code like:that is the proper image (or unitid) is stored at the position matching relevant
DisplayUnitsintrepresentation.Then you’ll be able to have the desired method work like this:
So the only things you’ll need to edit, if the
DisplayUnitsenum is edited, arestring[] Imagesin this example)You needn’t to edit the bodies of the methods (
DisplayUnitsImage,DisplayUnitID, and others if any).Or even easier. If all your methods work similarly, that is simply concat the data field and unit representation, then you may have one method instead all of them:
and pass the appropriate array (like
Imagesin this example) asdataargument: