1

I have a class with n data members as follows :

public class Input
{
    int mode1  {get; set;}
    int geom2  {get; set;}
    int type3  {get; set;}
    int spacing4 {get; set;}
    int fluid5 {get; set;}
    int spec6 {get; set;}
    ...
    ...
    ...
    int data_n {get; set;}
} 

and I have a filled list of n int items.

List<int> dataList = new List<int>

Now I want to fill an object of class Input from dataList through iteration or through any other direct method will be helpful. Thank you

7
  • Do you mean var input = new Input { mode1 = dataList[i + 0], geom2 = dataList[i + 1], ..., where i starting at 0 is the input index in the data list, enabling you to handle multiple in a loop? What have you tried? Commented Jul 9, 2014 at 8:49
  • Yes @Nikhil I want the list to fill a single instance. Commented Jul 9, 2014 at 8:50
  • Are the integers in your list in the same order as the fields in your Input class? Commented Jul 9, 2014 at 8:50
  • @CodeCaster, my real class comprises 98 data members so I cant use this. Commented Jul 9, 2014 at 8:51
  • @Rahul: 98 data members? Commented Jul 9, 2014 at 8:55

6 Answers 6

3

Reflection can be useful here but you should definitely re-consider your design if you have 98 properties in one class.

var properties = typeof(Input)
                .GetProperties()
                .Where(p => p.PropertyType == typeof(int));

int i = 0;
foreach(var prop in properties)
    prop.SetValue(yourObject, dataList[i++]);

But this doesn't guarantee that each property will be assigned correctly because as already mentioned by @CodeCaster GetProperty method doesn't return the properties in particular order.And there is no way to determine the order and map values to the properties when you have List<int>, if you can use Dictionary<string, int> instead, where key is the property name, then you can set each property to the corresponding value.

Sign up to request clarification or add additional context in comments.

2 Comments

@CodeCaster I didn't know that GetProperties method does not return properties in a particular order. As for the elegant way, I was meant the use of reflection which give us all the properties of the class called input and then the assign to each of these propertied the corresponding value, without having to use the name of the property.
1

You could try something like this (object initializer):

Input input = new Input { mode1 = dataList[0], 
                          geom2 = dataList[1],  
                          type3 = dataList[2],
                          spacing4 = dataList[3],
                          fluid5 = dataList[4],
                          spec6 = dataList[5] };

3 Comments

Don't you need parenthesis to instantiate a new Input object? EDIT: I'm assuming you don't as R.T's answer has suggested the same.
@DeeMac no we don't need parenthesis. Please have a look here msdn.microsoft.com/en-us/library/bb384062.aspx. Thanks
@DeeMac no problem at all dude. This wasn't certainly a stupid comment.
0

You can do this from reflection using GetProperties method as others said but why not use a simple way to do this?

Input i = new Input();

i.mode1 = dataList[0];
i.geom2 = dataList[1];
i.type3 = dataList[2];
i.spacing4 = dataList[3];
i.fluid5 = dataList[4];
i.spec6 = dataList[5];

Comments

0

You can try something like this using the reflection :

var properties = typeof(Input).GetProperties();

for(int i = 0; i < properties.Count(); ++i)
{
    properties[i].SetValue(dataList[i]);
}

Comments

0

I'm not sure how you use your properties but you could alternatively have a list in your class and then set the whole list or add to that. Then you could use an enum to keep track of the values.

Something like this:

public class Input
{
    public enum MyValues
    {
        mode1 = 1,
        geom2 = 2,
        ...
    }
    public List<int> Data { get; set; }
}

Comments

-1
  class Layout
   {
int mode1 { get; set; }
int geom2 { get; set; }
int type3 { get; set; }
int spacing4 { get; set; }
int fluid5 { get; set; }
int spec6 { get; set; }

public int this[int number]
{

    get
    {
        if (number == 1)
            return mode1;
        else if (number == 2)
            return geom2;
        else if (number == 3)
            return type3;
        else if (number == 4)
            return spacing4;
        else if (number == 5)
            return fluid5;
        else if (number == 6)
            return spec6;
        else
            return -1;
    }
    set
    {
        if (number == 1)
            mode1 = value;
        else if (number == 2)
            geom2 = value;
        else if (number == 3)
            type3 = value;
        else if (number == 4)
            spacing4 = value;
        else if (number == 5)
            fluid5 = value;
        else if (number == 6)
            spec6 = value;
    }
}

iteration code

 Layout layout = new Layout();
foreach(int i in dataList)
 {
   layout[i]=dataList[i];
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.