如何使用循环来分配值。 [英] How to assign values using loop through.

查看:87
本文介绍了如何使用循环来分配值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
                    {

                    });
                }



我有类SectorInfo ..



如何分配值




I have class SectorInfo..

How to assign values

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





来自selecteddasiem.cells使用循环。



from selecteddasiem.cells using loop through.

推荐答案

不要这样做:如果你分配个人名字,那么它是PITA在循环中为它们赋值 - 你必须使用反射,这很慢而且很麻烦。将来很难维护......



相反,创建一个整数数组,并使用constance值来索引该数组:



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:

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.


这篇关于如何使用循环来分配值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆