繁体   English   中英

从字典中检索信息

[英]Retrieve The Information From Dictionary

我已将信息保存在字典中。 字典由用户更新。 我如何从字典中检索信息。 我已经完成了一些代码。 把我不知道如何从字典中获取信息。

     //Create the dictionaries 
     Dictionary<int, int> waytosave = new Dictionary<int, int>();
     Dictionary<int ,int> numberControls = new Dictionary<int,int>();

          private void btnRun_Click(object sender, EventArgs e) 
       { 
    ///Setting up the coordinates
       int xCoor; 
       int yCoor;
       Random coor = new Random();
       int value =7; 
       for (int x = 0; x < value; x++)
          { 
        //Creating Random NumeircalUpdown. 
       //Using those the user can change the values.
        NumericUpDown numiNumber = new NumericUpDown();   
        xCoor = coor.Next(0, 500);     
       yCoor = coor.Next(0, 500);              
       numiNumber.Name = x.ToString();       
       numiNumber.Location = new Point(xCoor, yCoor);  
       numiNumber.Size = new System.Drawing.Size(50, 15);    
        numiNumber.Maximum = 100;    
        numiNumber.Minimum = 0; 
      //Saveing the numericalUpdowns   
        numberControls.Add(x, 0); 
       this.pnlNodes.Controls.Add(numiNumber); 

      //Make it respond to the clicking event    
         numiNumber.Click += new EventHandler(GetNumUpDownValue); 
         } 
         }
      //Get the values for the NumericUpDown
    public void GetNumUpDownValue(object sender, EventArgs e)
     { 
     int iname = int.Parse(((NumericUpDown)sender).Name);
     int ivalue = (int)((NumericUpDown)sender).Value;
    //check and update the list 
     if (waytosave.ContainsKey(iname))
       {
        waytosave[iname] = ivalue;
       }
           else
           { 
          waytosave.Add(iname, ivalue);
               } 

            txtOutputs.Text += "\r\r\n" + "   Node # " + iname + " = " + waytosave[iname].ToString(); 
             }

             private void btnRoundRobin_Click(object sender, EventArgs e)         {
              //how can i get the saved information from the waytosave dictionary
       //Can you advise me please????. 
         }

这是一种方法,假设您实际上并不关心键的顺序。

// Untested Code

int index = -1;
private void btnRoundRobin_Click(object sender, EventArgs e)
{
    var keys = waytosave.Keys;
    if(keys.Count == 0) return;
    index = (index + 1) % keys.Count;

    int key = keys[index];
    int value = waytosave[key];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM