davids
davids

Reputation: 5577

Create dynamic string name and set the value in C#

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:

  1. There are a few exceptions from the field name in the db, so I cannot simply use the field name and have to use the switch.
  2. I also have to populate some non-standard strings for a couple of the fields from the db.
  3. I currently have 32 field names and that can grow, so reducing the length of code will make it so much easier to manage. (@Servy's approach reduced it down to 19)
  4. The resulting strings are used in the view.

How do I dynamically set the string name and populate it?

Upvotes: 1

Views: 1126

Answers (3)

Servy
Servy

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

Steve
Steve

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

John
John

Reputation: 631

Use a Dictionary as the backing store for the Properties.

Upvotes: 0

Related Questions