Reputation: 5577
I have a list of strings like:
public string TypeOrig { get; set; }
public string TypeAdj { get; set; }
public string TypeClass { get; set; }
public string TypeMsg { get; set; }
public string QtyOrig { get; set; }
public string QtyAdj { get; set; }
public string QtyClass { get; set; }
public string QtyMsg { get; set; }
Based on the row from the db, I populate the strings like this:
switch(fRow["fieldName"].ToString())
{
case "partType":
TypeOrig = fRow["original"].ToString();
TypeAdj = fRow["adjusted"].ToString();
TypeClass = fRow["status"].ToString();
TypeMsg = fRow["message"].ToString();
break;
case "qty":
QtyOrig = fRow["original"].ToString();
QtyAdj = fRow["adjusted"].ToString();
QtyClass = fRow["status"].ToString();
QtyMsg = fRow["message"].ToString();
break;
}
I would like to reduce this to something like this:
switch(fRow["fieldName"].ToString())
case "partType": fieldName = "Type";
break;
case "qty": fieldName = "Qty";
break;
}
fieldName + "Orig" = fRow["original"].ToString();
fieldName + "Adj" = fRow["adjusted"].ToString();
fieldName + "Class" = fRow["status"].ToString();
fieldName + "Msg" = fRow["message"].ToString();
Additional Information:
How do I dynamically set the string name and populate it?
Upvotes: 1
Views: 1126
Reputation: 203802
You should refactor your object. Whenever you see yourself prefixing a bunch of fields/properties with the same thing it often manes you should be creating a new type to represent that kind of data.
public class Foo //TODO give meaningful name
{
//I changed the abbreviations to real words. If I was wrong, please fix, but
//as a rule try to avoid overuse of abbreviations in variable names;
//it makes code harder to read.
public string Original { get; set; }
public string Adjacent { get; set; }
public string Class { get; set; }
public string Message{ get; set; }
}
Now back to your first type we can have:
public class MyClass //TODO fix name
{
public Foo Type {get;set;}
public Foo Quality {get;set;}
}
Now that we have two objects of the same type we can separeate out the code for populating the object and placing that object in MyClass
:
Foo foo = new Foo()
{
Original = fRow["original"].ToString(),
Adjacent = fRow["adjusted"].ToString(),
Class = fRow["status"].ToString(),
Message= fRow["message"].ToString(),
};
switch(fRow["fieldName"].ToString())
{
case "partType":
Type = foo;
break;
case "qty":
Quality = foo;
break;
}
Upvotes: 3
Reputation: 216243
Seems a good use case for a Dictionary<string, string>
For example
Dictionary<string, string> fieldValues = new Dictionary<string, string>();
fieldValues.Add("TypeOrig", string.Empty);
fieldValues.Add("TypeAdj", string.Empty);
fieldValues.Add("TypeClass", string.Empty);
fieldValues.Add("TypeMsg", string.Empty);
fieldValues.Add("QtyOrig", string.Empty);
fieldValues.Add("QtyAdj", string.Empty);
fieldValues.Add("QtyClass", string.Empty);
fieldValues.Add("QtyMsg", string.Empty);
....
switch(fRow["fieldName"].ToString())
{
case "partType": fieldName = "Type";
break;
case "qty": fieldName = "Qty";
break;
default: fieldName = string.Empty;
break;
}
if(fieldName.Length > 0)
{
fieldValues[fieldName + "Orig"] = fRow["original"].ToString();
fieldValues[fieldName + "Adj"] = fRow["adjusted"].ToString();
fieldValues[fieldName + "Class"] = fRow["status"].ToString();
fieldValues[fieldName + "Msg"] = fRow["message"].ToString();
}
Of course, this change requires a rewrite of the code that uses directly the strings variables, but it has many advantages. Only one variable, extensibility with new keys, more object oriented, many functionalities applicable to dictionaries, extensibility with Linq....
Upvotes: 0