2 Replies - 1012 Views - Last Post: 24 March 2010 - 07:39 PM Rate Topic: -----

#1 nyxynyx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-March 10

List<T>

Posted 24 March 2010 - 06:54 PM

Hello!

I would like to use the following function to sort my listbox, but I'm not familiar with what <T> is. How do i use the function on my listbox?

private List<T> MixList<T>(List<T> inputList)
        {
            List<T> randomList = new List<T>();

            Random r = new Random();
            int randomIndex = 0;
            while (inputList.Count > 0)
            {
                randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
                randomList.Add(inputList[randomIndex]); //add it to the new, random list
                inputList.RemoveAt(randomIndex); //remove to avoid duplicates
            }

            return randomList; //return the new random list
        }




I tried the following but the compiler says that I need to specify the type explicitly. What should i do?

lst = MixList(lst);



Is This A Good Question/Topic? 0
  • +

Replies To: List<T>

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5259
  • View blog
  • Posts: 14,074
  • Joined: 18-April 07

Re: List<T>

Posted 24 March 2010 - 07:35 PM

A Listbox is not a List. A Listbox is a windows form control while a List is a data structure. Now if you need something similar you could use this which is based on the same principle...

private void MixList(ListBox inputList, int timeToMix) {
     Random r = new Random();
     int randomIndex = 0;
     int counter = 0;

     while (counter < timeToMix) {
          randomIndex = r.Next(0, inputList.Items.Count); //Choose a random object in the list

          inputList.Items.Add(inputList.Items[randomIndex]); //add it to the new, random list
          inputList.Items.RemoveAt(randomIndex); //remove to avoid duplicates

          counter++;
     }
}



You will notice the similarities here. You essentially give it a listbox and the number of times it should randomly pick an item from the list and send it to the end. It isn't the most efficient way, but it does pretty much the same thing the other function does with lists. Where it builds a new list and returns it, we use the source and the destination list as the same listbox.

Play with it, see if it works out for you purposes. Otherwise you can't use the other function on listboxes. Which is why you were having trouble. Define a List as a specific type like List<string> myList = new List<string>(); and then you can give it to your mixlist and it will work fine.

If you want to know more about <T> etc, read up on "Generics" in C#. Essentially <T> represents a data type. In my example above, <T> would have been <string>. Saying I have a list of string data types.

Enjoy! :)

This post has been edited by Martyr2: 24 March 2010 - 07:37 PM

Was This Post Helpful? 1
  • +
  • -

#3 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: List<T>

Posted 24 March 2010 - 07:39 PM

*EDIT* Ahh...Martyr2 beat me to it. :)

This post has been edited by lesPaul456: 24 March 2010 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1