Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So far here's what I have:

C#
string[] modes = {"AA", "BB", "CC"};
Label[] aName = new Label[5];

aName[0] = lblName1;
//Store label from form
//So on & so forth


            for (int y = 0; y < modes.Length; y++)
            {
                switch (modes[y])
                {
                    case "AA":
                        foreach (KeyValuePair<string, int> pair in dictionaryA)
                        //tried foreach (KeyValuePair<string, int> pair in dictionaryA).Take(5)), but it did not work?

                        {
                            u++;

                            if (u == 5)
                            {
                                return;
                            }
                                aName[u].Text = pair.Key.ToString().ToUpper();

                        }
                        break;
                     .
                     .
                     .
                     .
                }
            }


I need to get only the first 5 items of the Dictionary and output each item to separate labels. The code structure looks ridiculous but these are only my assumptions based on the basic knowledge I have of C#. :D :)
Posted
Updated 2-Oct-14 2:47am
v4

I would use a for loop, e.g.
C#
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("a", 1);
dict.Add("b", 2);
dict.Add("c", 3);

for (int index = 0; index < 5 && index < dict.Count; index++)
{
  Console.WriteLine(dict.ElementAt(index).Key.ToUpper());
}
 
Share this answer
 
I have modified your code with a console application to demonstrate and it works fine.Hope this helps to you :-)

C#
static Dictionary<string,> myDictionary = new Dictionary<string,>();

       static void Main(string[] args)
       {
           myDictionary.Add("1", 4);
           myDictionary.Add("2", 3);
           myDictionary.Add("3", 1);
           myDictionary.Add("4", 5);
           myDictionary.Add("5", 6);
           myDictionary.Add("6", 7);
           myDictionary.Add("7", 8);
           myDictionary.Add("8", 9);

           string[] modes = { "AA", "BB", "CC" };
           string[] arr = new string[5];
           int u = 0;

           for (int y = 0; y < modes.Length; y++)
           {
               switch (modes[y])
               {
                   case "AA":
                       foreach (KeyValuePair<string,> pair in myDictionary)
                       {


                           if (u < 5)
                           {
                               arr[u] = pair.Key.ToString().ToUpper();
                           }
                           else
                           {
                               continue;
                           }
                           u++;
                       }
                       break;
               }
           }

          foreach(string k in arr)
          {
              Console.WriteLine(k);
          }

          Console.ReadLine();
       }
   }
 
Share this answer
 
v2
Comments
kmllev 2-Oct-14 6:47am    
Thank you for answering! However, when I runthe debugger and set a breakpoint at the first foreach loop, it doesn't run the code inside the loop. D:
Dilan Shaminda 2-Oct-14 6:58am    
i checked my answer again.It print first 5 elements of the Dictionary. Are you getting any errors?
kmllev 2-Oct-14 7:22am    
Never mind, Dilan! I ran the debugger again and fixed the issues. Thank you so much! :) :)
Dilan Shaminda 2-Oct-14 7:32am    
you are welcome :-)
I don't see anything wrong with your code example ... but ... I can't see where the variable 'u is initialized; I can only assume it's initial value is #0.

Here's an alternative example using Linq:
C#
private Dictionary<string, int> DictionaryA = new Dictionary<string, int>();

// for testing  only
List<Label> ALabelList = new List<Label>();

// for testing only
for (int i = 0; i < 10; i++)
{
   DictionaryA.Add(i.ToString(), i);
}

foreach (var kvp in DictionaryA.Take(5))
{
    ALabelList.Add(new Label {Text = kvp.Key.ToUpper()});
}
For reasons why one might avoid using the Linq ForEach, and All operators: see Eric Lippert's article: [^].
 
Share this answer
 
v2
This may helpful for you here am taking repeater in that am took label for that label am assigning key values 


C#
private void BindDictionaruy()
        {

            Dictionary<string, int> dict = new Dictionary<string, int>();
            dict.Add("a", 1);
            dict.Add("b", 2);
            dict.Add("c", 3);
            dict.Add("3", 4);
            dict.Add("g", 5);

            var slist= dict.Take(5).Select(a => new { test =a.Key}).ToList();

            rptrBindDictionary.DataSource = slist;
            rptrBindDictionary.DataBind();

        }


XML
<asp:Repeater runat="server" ID="rptrBindDictionary">
          <ItemTemplate>
              <asp:Label Text='<%# Eval("test") %>' runat="server"  ID="lblBindDictionary"/>
          </ItemTemplate>
      </asp:Repeater>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900