In my program I have coded the following private variables and function.
private string _viewName;
private string _refValue;
private void getContentDetails(string id) {
switch (id.Substring(2, 2))
{
case "00": _refValue = "14"; _viewName = "Menu"; break;
case "01": _refValue = "18"; _viewName = "Topic"; break;
default: _refValue = "00"; _viewName = "Menu"; break;
}
}
I am looking for a better way to code this and I was thinking of replacing this with two private functions.
getViewName(id);
getRefValue(id);
Can someone suggest what might be the best way that I could code this?
It’s certainly not wrong to put more than one statement in a switch, but it looks like there could be some duplication here, especially if the needed values that are coupled with an ID are changed.
It seems to me that you would have to use a map collection object for both
viewNameandrefValuethat maps the id’s with the corresponding values. Then useyourCollection.get(id);. That will certainly keep your code best maintainable and consistent.This is not even yet the best solution. The best would be that you use a dictionary to couple the refValues to the viewNames, and then another dictionary that maps the ids to the refvalues. You can then get the refValue by using the id as key, and then the returned refValue as the key to get the viewName. Hope I made myself clear 🙂