0

listViewコントロールにforeachループがあり、listViewの内容ごとにオブジェクトを作成したいので、foreachループでオブジェクトの名前を段階的に変更したい

foreach (var item in listViewStates.Items)
            {
               State s = new State 
               {
                   ID = MaxStateID,
                   Name = listViewStates.Items[0].Text,
                   WorkflowID = MaxWFID,
                   DueDate = Convert.ToInt32(listViewStates.SelectedItems[0].SubItems[1].Text),
                   Priority = Convert.ToInt32(listViewStates.SelectedItems[0].SubItems[2].Text),
                   RoleID = Convert.ToInt32(listViewStates.SelectedItems[0].SubItems[3].Text),
                   Status =Convert.ToInt32(listViewStates.SelectedItems[0].SubItems[4].Text)
               };
               i++;
            }

変数は State クラスの s です

4

1 に答える 1

2

間違ったアプローチをしている可能性があります。状態オブジェクトに対して行う必要があるのは、それをコレクションに追加し、そこから操作することです。この方法で追跡する方がはるかに簡単です。

関数内のループ後に使用するローカル リストの例:

public void MyFunction()
{
    List<State> states = new List<State>();

    foreach (var item in listViewStates.Items)
    {
        State s = new State
        {
            //Set state properties
        };
        states.Add(s);
    }
    //Use your states here, address with brackets
    //states[0].ID ...
}

関数の外で後で使用するためのクラスレベルのリストの例:

List<State> _states;

public void MyFunction()
{
    _states = new List<State>();
    foreach (var item in listViewStates.Items)
    {
        State s = new State
        {
            //Set state properties
        };
        _states.Add(s);
    }
    //Now, after calling the function, your states remain
    //You can address them the same way as above, with brackets
    //_states[0].ID ...
}
于 2013-07-29T14:19:08.567 に答える