8 Replies - 14590 Views - Last Post: 05 April 2011 - 09:29 AM Rate Topic: -----

#1 hopefree   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-March 11

get value from sortedList

Posted 05 April 2011 - 03:52 AM

i have a sortedList, may i know how to get value from that sortedList ?

the code below are the code for sortedList.

foreach (String sort in lbCombinations.Items)
{


if (!(sortList.ContainsKey(sort)))
{
sortList.Add(sort, 1);
}
else
{
int Count = (int)sortList[sort];
sortList[sort] = Count + 1;
}

}

IDictionaryEnumerator enumerator = sortList.GetEnumerator();
richTextBox1.Clear();
while (enumerator.MoveNext())
{
richTextBox1.AppendText("(" + enumerator.Key.ToString() + "): " + " " + enumerator.Value.ToString() + "\n");

This post has been edited by Curtis Rutland: 05 April 2011 - 06:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: get value from sortedList

#2 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: get value from sortedList

Posted 05 April 2011 - 04:15 AM

I am having some issues wrapping my head around what you are going for here....Seems like you are trying to count how often something shows up in your list box and then display the results in a text box. Is that about right? If so, I am not sure of the question. Where is the error, or the uncertainty, coming from?
Was This Post Helpful? 0
  • +
  • -

#3 marinus   User is offline

  • D.I.C Addict
  • member icon

Reputation: 138
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: get value from sortedList

Posted 05 April 2011 - 04:23 AM

To get the value of sorted list you could use this code

  SortedList<int, string> list = new SortedList<int, string>() ;
            list.Add(1, "fjkd");
            list[1].ToString();//Get the value




or you can use linq like this

 SortedList<int, string> list = new SortedList<int, string>();
            list.Add(1, "fjkd");
            list[1].ToString();


            var slist = from l in list.AsEnumerable() 
                        where l.Key.Equals(1) 
                        select l.Value;

            foreach (var item in slist)
                MessageBox.Show(item);
//Gets the value

This post has been edited by marinus: 05 April 2011 - 04:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: get value from sortedList

Posted 05 April 2011 - 05:45 AM

list[1].ToString();
It's a string already, you declared it as such. Don't call ToString() on strings.
Was This Post Helpful? 0
  • +
  • -

#5 ashley11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-April 11

Re: get value from sortedList

Posted 05 April 2011 - 08:21 AM

my sortedLIst and the code are show below:
(1): 4
(2): 4
(1 2): 3
(3): 4
(1 3): 3
(2 3): 3
(1 2 3): 2

lbCombinations.Items.Clear();
int n = txtCaseInputs.Lines.Length;
for (int k = 1; k <= txtCaseInputs.Lines.Length; k++)
{
txtNumCombinations.Text = Combination.Choose(n, k).ToString();
Combination c = new Combination(n, k);
string[] result = new string[k];
while (c != null)
{
result = c.ApplyTo(txtCaseInputs.Lines);

StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; ++i)
{
sb.AppendFormat("{0}{1}", result[i], " ");
}
lbCombinations.Items.Add(sb.ToString().Trim());

c = c.Successor();
}
}

Display();
}
private void Display()
{

foreach (String sort in lbCombinations.Items)
{
if (!(sortList.ContainsKey(sort)))
{
sortList.Add(sort, 1);
}
else
{
int Count = (int)sortList[sort];
sortList[sort] = Count + 1;
}
}

IDictionaryEnumerator enumerator = sortList.GetEnumerator();
richTextBox1.Clear();
while (enumerator.MoveNext())
{
richTextBox1.AppendText("(" + enumerator.Key.ToString() + "): " + " " + enumerator.Value.ToString() + "\n");

}
}


now my problem is how to extract some itemset from the sortedlist above?
my output should be like this:
(1 2): 3
(1 3): 3
(2 3): 3
(1 2 3): 2
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: get value from sortedList

Posted 05 April 2011 - 08:31 AM

Please don't start multiple threads for the same thing:
http://www.dreaminco...m_notifications
http://www.dreaminco...m_notifications

And starting multiple thread using two different identities is a major no-no here.
Was This Post Helpful? 0
  • +
  • -

#7 ashley11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-April 11

Re: get value from sortedList

Posted 05 April 2011 - 08:37 AM

we 2 doing a similar project. at the beginning, both of us are same, but after some part, we are distint
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: get value from sortedList

Posted 05 April 2011 - 09:09 AM

Code in this post:

Quote

IDictionaryEnumerator enumerator = sortList.GetEnumerator();
42 richTextBox1.Clear();
43 while (enumerator.MoveNext())
44 {
45 richTextBox1.AppendText("(" + enumerator.Key.ToString() + "): " + " " + enumerator.Value.ToString() + "\n");
47 }



Code from the other post:

Quote

IDictionaryEnumerator enumerator = sortList.GetEnumerator();
18 richTextBox1.Clear();
19 while (enumerator.MoveNext())
20 {
21 richTextBox1.AppendText("(" + enumerator.Key.ToString() + "): " + " " + enumerator.Value.ToString() + "\n");
22 }


Either
  • your both stealing from the same source
  • one of you is stealing from the other
  • you're both the same person
  • you're both on the same team for this school work.


Either way, I think these thread should at the minimum be merged, but more likely locked and maybe even closed. There's something fishy going on here. In a best case you're working on the same homework assignment which means we can't help you do our schoolwork anyway.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: get value from sortedList

Posted 05 April 2011 - 09:29 AM

Merged duplicate topics. If you're really different people, why would you open up a new topic with the same code? What was the point?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1