开发者

Assigning values to lists within lists within lists within lists

开发者 https://www.devze.com 2023-01-20 09:45 出处:网络
I have a hierarchy of lists within lists, and at the very bottom I\'m assigning a decimal value.I need to assign this value based on the combination of the lists that are above it in the hierarchy.Eac

I have a hierarchy of lists within lists, and at the very bottom I'm assigning a decimal value. I need to assign this value based on the combination of the lists that are above it in the hierarchy. Each element of a particular list is an Enum value, so o开发者_JAVA百科ne combination could be Trees, Buildings, BuildingNumber. And another Trees, Buildings, BuildingPrice. What would be the best way of assigning values to these combinations?

The values I'm assigning from are coming from just an arbitrary class. I will then need to do some calculations to some of those values to assign the new values in a particular combination.

public class Hierarchy1
{
    public List<Hierarchy2> NextList;
    public Hierarchy1Enum EnumValue;
}

public class Hierarchy2
{
    public List<Hierarchy3> NextList;
    public Hierarchy2Enum EnumValue;
}

public class Hierarchy3
{
    public decimal Value;
    public Hierarchy3Enum EnumValue;
}

Something like that. The Enums each have 5-10 values so there's quite a few combinations.

Hierarchy1 will have a list of Hierarchy2's. If there are 10 values in the Hierarchy2Enum, there will be 10 elements in that List (so essentially 1 element per enum).


Do you mean something like this?

static void Main(string[] args)
{
    var list = new List<Hierarchy1>();

    foreach (Hierarchy1Enum enum1 in Enum.GetValues(typeof(Hierarchy1Enum)))
    {
        var item1 = new Hierarchy1();

        list.Add(item1);

        item1.EnumValue = enum1;
        item1.NextList = new List<Hierarchy2>();

        foreach (Hierarchy2Enum enum2 in Enum.GetValues(typeof(Hierarchy2Enum)))
        {
            var item2 = new Hierarchy2();

            item1.NextList.Add(item2);

            item2.EnumValue = enum2;
            item2.NextList = new List<Hierarchy3>();

            foreach (Hierarchy3Enum enum3 in Enum.GetValues(typeof(Hierarchy3Enum)))
            {
                var item3 = new Hierarchy3();

                item2.NextList.Add(item3);

                item3.EnumValue = enum3;

                item3.Value = GetValue(enum1, enum2, enum3);
            }
        }
    }
}

private static decimal GetValue(Hierarchy1Enum enum1, Hierarchy2Enum enum2, Hierarchy3Enum enum3)
{
    return // Calculate your value from enum1, enum2 and enum3
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号