Re: Не работает foreach... как правильно?
От: Vermicious Knid  
Дата: 04.06.07 15:12
Оценка: 12 (1)
Здравствуйте, FDSC, Вы писали:

FDS>foreach(N in inputData.NodeList)

Внутри образцов все, что начинается с большой буквы, считается именем типа.

FDS>// Ошибка pattern matches 2 values, while the type 'System.Collections.Generic.KeyValuePair' has 0 fields

FDS>foreach((key, value) in inputData.NodeList)
(key, value) это образец для кортежа. Еще он подходит для типов(классов/структур/вариантов) с 2-мя публичными полями. У KeyValuePair публичных полей нет.

Можно написать так
foreach(n in inputData.NodeList)
// или
foreach((Key = key, Value = value) in inputData.NodeList)
Не работает foreach... как правильно?
От: FDSC Россия consp11.github.io блог
Дата: 04.06.07 14:30
Оценка:
Пытаюсь обойти Dictionary с использованием foreach...
// System.Collections.Generic.KeyValuePair[int, XmlIOsystem.OutputClass.NodeStruct] 


// Ошибка unbound type name N
foreach(N in inputData.NodeList)
{
}

// Ошибка pattern matches 2 values, while the type 'System.Collections.Generic.KeyValuePair' has 0 fields
foreach((key, value) in inputData.NodeList)
{
}

комментарии в коде показывают, какие ошибки появляются

Вот сокращённое объявление Dictionary на C#

// ...
namespace XmlIOsystem
{
    public class OutputClass
    {
       // ...
        public struct NodeStruct
        {
            // ....
        }

// ....

        public Dictionary<int, NodeStruct>           NodeList = new Dictionary<int, NodeStruct>();
        // ....

        public void SetDictionaries(InputDataForm IDF, DataGridView NodesDGV,      DataGridView BarsDGV,
                                                       DataGridView CrossAreasDGV, DataGridView MaterialsDGV, DataGridView LoadsDGV)
        {
            SetFuncsDictionary     (IDF);
            SetNodesDictionary     (NodesDGV);
            SetBarsDictionary      (BarsDGV);
            SetCrossAreasDictionary(CrossAreasDGV);
            SetMaterialsDictionary (MaterialsDGV);
            SetLoadsDictionary     (LoadsDGV);
        }

        protected NodeStruct GetNodeStruct(int i, DataGridViewRow Row)
        {
            NodeStruct S = new NodeStruct( (double)   Row.Cells[01].Value, (double)   Row.Cells[02].Value, (double)   Row.Cells[03].Value,
                                           (Vector3d) Row.Cells[04].Value, (Vector3d) Row.Cells[05].Value, (Vector3d) Row.Cells[08].Value,
                                           (Vector3d) Row.Cells[09].Value, (Vector3d) Row.Cells[06].Value, (Vector3d) Row.Cells[07].Value,
                                           (bool)     Row.Cells[10].Value, (bool)     Row.Cells[11].Value);

            return S;
        }

        protected void SetNodesDictionary(DataGridView NodesDGV)
        {
            for (int i = 0; i < NodesDGV.RowCount; i++)
            {
                NodeList.Add( (Int32) NodesDGV.Rows[i].Cells[0].Value, GetNodeStruct(i, NodesDGV.Rows[i]) );
            }
        }

// ....
    }
}
Re: Не работает foreach... как правильно?
От: Kisloid Мухосранск  
Дата: 04.06.07 14:53
Оценка:
Здравствуйте, FDSC, Вы писали:


Попробуй так:
using System;
using System.Console;
using Nemerle.Collections;

module M {
  public Main () : void {
    def dict = System.Collections.Generic.Dictionary();
    dict.Add(1, "One");
    dict.Add(2, "Two");
    dict.Add(3, "Three");
    foreach (e : System.Collections.Generic.KeyValuePair [int, string] in dict)
    {
        WriteLine((e.Key :> int).ToString() + " " + e.Value :> string);
    }
  }
}
((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x))))
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.