Ben
Ben

Reputation: 25

C# How to use variables as property names in an object List object constructor for key-value string arrays

Background:

I have parsed key-value pairs that I imported into objects. I did this because I want to be able to sort by the name properties which the logic is easy for:

var varDupe = variableList.GroupBy(v => v.varName).Where(vName => vName.Count() > 1).Select(vName => vName.Key);

I debated between these and a dictionary list, but in the end it seemed like searching was easier as I don't need to export these key-value pairs, only error-check them. I will need a lot of dependencies like "properties X,Y, and Z is required if property Q exists" for a few of my object lists and search through all of the distinct objects at once. If I would be better of with a list of dictionaries, please let me know.

My code works perfectly now with correctly sequenced key-value pairs, but as key value pairs, they inherently don't need to be grouped in the correct order. I made a sorting function for my parsed key-value string arrays

Issue

That function sorts the string arrays, but the problem comes when I have missing keys (for example, if varVal is optional). varVal and varParam should both be doubles, so I need to convert them in the object

Assume I have the string array leaving out an optional parameter.

varName,name1,varParam,1.01

The "full" array would look like this:

varName,name1,varVal,3.9,varParam,1.01

I have this code for building my objects:

 switch (sortedVals.Count())
                {
                    case 2:
                        materialList.Add(new Variable(sortedVals[1]));
                        break;
                    // Example of what I am trying to do to use the key-values to identify the correct property to assign a value to
                    case 4:
                        materialList.Add(new Variable(sortedVals[1]) { sortedVals[2] = Convert.ToDouble(sortedVals[3]) } );
                        break;
                    // Example of how it works correctly if they are all in order
                    case 6:
                        materialList.Add(new Variable(sortedVals[1], Convert.ToDouble(sortedVals[3]), Convert.ToDouble(sortedVals[5]) ) );
                        break;
                    ...

If you look at "case 4" I am trying to use the "key" from string array to assign the correct "value" to. My Variable constructor inside my Variable class looks like this:

public Variable(string varName, double varVal = 1, double varParam = 0)

and

sortedVals[2] = "varParam" 

The compiler doesn't recognize that my variable is intended to be used as a string for the constructor name (i.e. varParam = Convert.ToDouble("1.01") in case 4).

How can I use a string variable like this as a property name in a constructor?

Would I be better off scrapping the objects and trying to make a dictionary list and finding a way to search for name duplicates and other properties?

Thanks everyone!

Upvotes: 0

Views: 1151

Answers (1)

Homungus
Homungus

Reputation: 1114

There are different ways to achieve this:

This is a very basic solution, no linq etc. and assuming your split is always representing a single object:

string name; 
double val = 1;
double val2 = 0;
for (int i = 0; i < sortedVals.length; i++)
{
    string key = sortedVals[i];
    i++;
    string valString = sortedVals.length > i ? sortedVals[i] : string.Empty;

    switch (key) 
    {
         case "varName":
             name = valString;
             break;
         case "varVal":
             val = valString != string.Empty ? double.Parse(valString) : val;
             break;
         case "varParam":
             val2 = valString != string.Empty ? double.Parse(valString) : val2;
             break;
    }
}
materialList.Add(new Variable(name, val, val2));

Upvotes: 1

Related Questions