如何将硬编码值列表转换为使用Add()方法c#?

时间:2016-08-29 11:27:51

标签: c# list

我想将硬编码值列表转换为使用Add()方法但是,我不能这样做。我正在为它编写一些代码,但它不起作用。 我把原始代码更好地理解。实际上,*pictureBox1_MouseClick*是原始代码。

 private List<List<Node>> graph = new List<List<Node>>();
        private List<Node> ListNode = new List<Node>();
        private Node node = new Node();
 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {


        if (e.Button != MouseButtons.Right)
        {
            var result = circleManager.HitTest(e.Location);
            if (result != -1)
            {
                circlesSourceAndDestination.Add(circleManager.Circles[result]);
                if (Count == 1)
                {
                    node.Id = result;
                }
                else if (Count == 2)
                {
                    var weigth = CalculateLengthSourceAndDestination(circlesSourceAndDestination);
                    if (weigth < 0)
                    {
                        weigth *= -1;
                    }
                     ListNode.Add(node);

                    graph.Add(ListNode);


                    Count = 0;
                }
                Count++;
            }
        }
        else
        {
            var result = circleManager.HitTest(e.Location);
            if (result != -1)
            {
                circleManager.Circles[circleManager.HitTest(e.Location)].Selected = true;
                circleManager.Circles[result].SelectFillColor = Color.Red;
            }
        }
        pictureBox1.Invalidate();
    }
List<Node> graph = new List<Node>() {
            new Node() {
                Id = 0, Neighbors = new List<KeyValuePair<int,int>>() {
                   new KeyValuePair<int,int>( 1,127),
                   new KeyValuePair<int,int>( 2,100),
                   new KeyValuePair<int,int>( 4,139),
                   new KeyValuePair<int,int>( 6,117),
                   new KeyValuePair<int,int>( 7,156)
                }
            },
            new Node() {
                Id = 1, Neighbors = new List<KeyValuePair<int,int>>() {
                   new KeyValuePair<int,int>( 0,127),
                   new KeyValuePair<int,int>( 2,102),
                   new KeyValuePair<int,int>( 3,108),
                   new KeyValuePair<int,int>( 7,53)
                }
            }

    public class Node
{
    public Node()
    {
        Neighbors = new List<KeyValuePair<int, int>>();
    }

    public int Id { get; set; }
    public List<KeyValuePair<int, int>> Neighbors { get; set; }
}
}

2 个答案:

答案 0 :(得分:1)

您可以在图表中声明新的Node.Add(),如下所示:

Node newNode = new Node();
//Assign Node Id
newNode.Id = 2; 
//Declare a new List for Neighbors in Node
List<KeyValuePair<int, int>> neighborList = new List<KeyValuePair<int, int>>();
//Add members to neighborList
neighborList.Add(new KeyValuePair<int, int>(1, 1)); //Assign these values dynamically;
neighborList.Add(new KeyValuePair<int, int>(2, 2)); 
neighborList.Add(new KeyValuePair<int, int>(3, 3));
//Assign neighborList to Neighbors
//Note that this could have been done without declaring a separate List
newNode.Neighbors = neighborList;
//Add newNode to your List
graph.Add(newNode);

答案 1 :(得分:0)

您可以使用Dictionary<int, int>代替List<KeyValuePair<int,int>>。然后,您可以执行以下操作:

public class Node {
    ...
    public Dictionary<int, int> Neighbors { get; set; }
}


Neighbors.Add(0,127);
Neighbors.Add(3,108);
...