Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public class SectorInfo
        {
            public int Sector1 { get; set; }
            public int Sector2 { get; set; }
            public int Sector3 { get; set; }
            public int Sector4 { get; set; }
            public int Sector5 { get; set; }
            public int Sector6 { get; set; }
            public string Name { get; set; }
            public SectorInfo(string name,int sector1,int sector2,int sector3,int sector4,int sector5,int sector6 )
            {
                Name = name;
                Sector1 = sector1;
                Sector2 = sector2;
                Sector3 = sector3;
                Sector4 = sector4;
                Sector5 = sector5;
                Sector6 = sector6;
            }
        }



foreach (var cells in selecteddashitem.Cells)
                {
                    sectorList.Add(new SectorJson
                    {

                    });
                }

I have class SectorInfo..

How to assign values

CSS
Name = name;
                Sector1 = sector1;
                Sector2 = sector2;
                Sector3 = sector3;
                Sector4 = sector4;
                Sector5 = sector5;
                Sector6 = sector6;


from selecteddasiem.cells using loop through.
Posted
Comments
Sinisa Hajnal 24-Mar-15 7:31am    
Winforms? Web (probably due to Json)? WPF? What are the cells? From DataGrid? You'll have to add more details...also, not clear what do you need? If you need to create JSON from your object, either use a library (for example Newtonsoft.Json) or create your own string.

1 solution

Don't do it like that: if you assign individual names, then it's a PITA to assign values to them in a loop - you have to use reflection and that's slow and cumbersome. It's also difficult to maintain in future...

Instead, create an array of integers, and have constance values to index into that array:

C#
public const int SectorCount = 6;
public const int Sector1 = 0;
public const int Sector2 = 1;
public const int Sector3 = 2;
public const int Sector4 = 3;
public const int Sector5 = 4;
public const int Sector6 = 5;
public int[] Sectors = new int[SectorCount];

public void DoIt()
    {
    int sector = Sectors[Sector3];
    ...


Then you can easily assign the array elements in a loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900