How to make a List<T> out of a Custom Class?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1440 Views - Last Post: 23 July 2011 - 05:06 PM Rate Topic: -----

#1 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 03:40 PM

I have this class
    class Variable
    {
        char ascii;
        double probabilidad;
        int codigo;

        public Variable()
        { }
        public void llenar(char letra, double prob)
        {
            ascii = letra;
            probabilidad = prob;
        }

        public double Probabiliada
        {
            get
            {
                return probabilidad;
            }
        }

        public int Codigo
        {
            set
            {
                codigo = value;
            }
            get
            {
                return codigo;
            }
        }

        public char Caracter
        {
            get
            {
                return ascii;
            }
        }
    }

I'm making a List<T>, with T being "Variable" but when I add an item to it, it overwrites the last one and also adds one. Basicly what happends is that
//tempVariable = char 'a', double 0.1, int 1
myListT.Add(tempVariable); //now i have an item type "Variable" in my list so I add another one
//tempVariable = char 'b', double 0.2, int 1
myListT.Add(tempVariable);//add a second "Variable"


what i have at the end is a list with alot of "variable" with values = char 'a', double '0.2', int 1 in every single value in my list, they all become the last one.

Is This A Good Question/Topic? 0
  • +

Replies To: How to make a List<T> out of a Custom Class?

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 03:47 PM

I dont get what your problem is. T is the type parameter. Show the class that is the type parameter.

This post has been edited by darek9576: 22 July 2011 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 03:55 PM

Yes T is a Type the type is gonna be "Variable" which is the name of my class.

Thing is all the values i add to my list, it doesnt matter wich value, they all become the same as the last one that was added.

So the all the values of the list are the same. Of the List<Variable> all of them are the same.
Was This Post Helpful? 0
  • +
  • -

#4 wiero  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 03:55 PM

i think that you do not create new tempVariable just change its values and then you add item to list. so you're adding same object all the time, but i may be wrong.please post more code of adding and creating classes

This post has been edited by wiero: 22 July 2011 - 03:56 PM

Was This Post Helpful? 1
  • +
  • -

#5 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 03:59 PM

You were right i wasn't using the new Variable, now i can make the list, the problem now is that i cant use List.Sort(), it says that if fails to compare two values, but i would like it to be sorted according to the double variable in the class.

Any ideas how to do this?

@Curtis: yes i made the correction, noticed it as well, i think im tired and i missed that. Any ideas in the other problem?

This post has been edited by poncho4all: 22 July 2011 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,719
  • Joined: 08-June 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 03:59 PM

I'm also going to chime in and say post more code.

I think it's because Classes are reference types, you're actually modifying the instance that you added and adding it again, rather than adding a new instance. Basically, if you're still using tempVariable and not calling new between the adds, this is what's wrong.
Was This Post Helpful? 1
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:00 PM

Good point. You need to create unique objects and add them to the collections. Give us teh code.
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:02 PM

Yep, you're adding the same object to the List<Variable> many times.

As for sorting goes, a quick cheap (kinda dirty) way to do this is by using a delegate:

myList.Sort(delegate(Variable v1, Variable v2) { return v2.Probabilidad.CompareTo(v1.Probabilidad); });



The more correct way is to implement IComparable in your custom types.

More info here: http://msdn.microsof...y/b0zbh7b6.aspx

Specifically, here: http://msdn.microsof...y/4d7sx9hd.aspx

This post has been edited by Sergio Tapia: 22 July 2011 - 04:04 PM

Was This Post Helpful? 1
  • +
  • -

#9 wiero  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:03 PM

your class should implement IComparable<T> interface

This post has been edited by wiero: 22 July 2011 - 04:04 PM

Was This Post Helpful? 3
  • +
  • -

#10 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:03 PM

Check out IComparable<> interface and CompareTo() method.
Was This Post Helpful? 0
  • +
  • -

#11 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:05 PM

Could you explain a bit how that works Sergio Tapia, i dont really understand. The Sort part please.

Edit: thank you all, I posted this one before i refreshed my page. Im gonna check the IComparable.

IComparable worked just fine, thanks all for the help.

This post has been edited by poncho4all: 22 July 2011 - 04:13 PM

Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,719
  • Joined: 08-June 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:05 PM

IComparable is fine. You can also do things the LINQ way with OrderBy.
Was This Post Helpful? 1
  • +
  • -

#13 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:06 PM

Do you know Google?
Was This Post Helpful? 0
  • +
  • -

#14 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:07 PM

Yeah, I'd say forget my delegate and use OrderBy, it's pretty darn useful and *easier to maintain.

myList.OrderBy(x => x.Probabilidad);

This post has been edited by Sergio Tapia: 22 July 2011 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,719
  • Joined: 08-June 10

Re: How to make a List<T> out of a Custom Class?

Posted 22 July 2011 - 04:23 PM

Of course, you can't do that in a vacuum. OrderBy returns a sorted IEnumerable, it doesn't modify the collection. So you'd have to .ToList it and assign it to the variable.

Also, you know you could use a lambda for your original delegate too, Sergio:

delegate(Variable v1, Variable v2) { return v2.Probabilidad.CompareTo(v1.Probabilidad); }


is the equivalent of

(v1, v2) => v2.Probabilidad.CompareTo(v1.Probabilidad)

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2