473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is this the correct behaviour (List<class> question)

Paul Johnson
Hi,

I enjoy trying new ways (to me) of doing getting my code to be more efficient. I'm currently working on a database system (front end to an access file with added bits) and have hit upon a problem.

I have a number of tutors containing 3 pieces of information (name, email, mobile number). There can be loads of these and rather than muck around, I've come up with the following solution.

My solution looks like this

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace class_list_test
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             tester m = new tester();
  12.             List<tester> t = new List<tester>();
  13.             m.a = 1;
  14.             m.c = "wibble";
  15.             m.d.Add(3.14);
  16.             m.d.Add(3.28);
  17.             t.Add(m);
  18.             m.a = 2;
  19.             m.c = "wobble";
  20.             m.d.Add(6.02);
  21.             m.d.Add(6.16);
  22.             t.Add(m);
  23.             m.a = 3;
  24.             m.c = "snurfle";
  25.             m.d.Add(9.44);
  26.             m.d.Add(9.58);
  27.             t.Add(m);
  28.             m.a = 4;
  29.             m.c = "snarfle";
  30.             m.d.Add(7.33);
  31.             m.d.Add(7.47);
  32.             t.Add(m);
  33.             printstuff(t);
  34.         }
  35.  
  36.         private static void printstuff(List<tester> r)
  37.         {
  38.             for (int n = 0; n < r.Count; ++n)
  39.                 Console.WriteLine("{0}, {1}, {2}, {3}", r[n].a, r[n].c, r[n].d[0], r[n].d[1]);
  40.             Console.ReadLine();
  41.         }
  42.     }
  43.  
  44.     public class tester
  45.     {
  46.         public int a;
  47.         public string c;
  48.         public List<double> d = new List<double>();
  49.     }
  50. }
  51.  
The code compiles and runs, but not as expected. The output of the code is four copies of the last things m is set to which doesn't seem to be the correct behaviour to me. I can get around it by inserting

Expand|Select|Wrap|Line Numbers
  1. m = new tester();
  2.  
after each add - but this seems wasteful to me.

Am I better off adding a get/set in the tester class will I still need a new instance of tester() after each add?

Paul
Oct 17 '11 #1

✓ answered by Rabbit

Define correct behavior. It's incorrect behavior based on your requirements. But it's correct behavior from the code that is written.

You've misunderstood how a list works. When you add an object to a list, it doesn't create a copy of it, it only stores a pointer to the object. Because you don't create a new object each time, you are adding the same pointer to the list.

2 1390
Rabbit
12,516 Expert Mod 8TB
Define correct behavior. It's incorrect behavior based on your requirements. But it's correct behavior from the code that is written.

You've misunderstood how a list works. When you add an object to a list, it doesn't create a copy of it, it only stores a pointer to the object. Because you don't create a new object each time, you are adding the same pointer to the list.
Oct 17 '11 #2
Thanks for the clarification - much appreciated :)
Oct 31 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Andrew Robinson | last post by:
I have two List<t>. I need to search ListA to see if it contains ListB So: ListA { 1, 2, 3, 4, 5, 6, 7, 8, 9 } Searching ListA with { 4, 5, 6 } would return true and an index of 3....
18
by: Sean | last post by:
I have been using List(of String) when I could easily be using a string array instead. Is it still considered best practice to use Generic list of string rather then a string array? Thanks
3
by: JB | last post by:
I've created a generic of type List<T> where T is a custom class. I need to use the List<T>.Contains method. I know I need to implement the IEqualityComparer but I can't seem to get the Contains...
1
by: Birthe Gebhardt | last post by:
Dear all, I could not find the way to handle 'not normal' list objects, for example using remove_if, find etc. Example : class Todo { public : .. int getNumber(){ return num_;}
22
by: Fabio Z | last post by:
Hi all, I have a classic problem: List<Tand List<Xwhere X is a class X : T. Ok, I know the problem: I cannot cast List<Ton List<Xbecause also if X is a T, List<Xis not a List<T>. What I don't...
1
by: Niels Ull | last post by:
Hi! I am looking for a simple way to validate members in my own System.Collections.Generic.List<> derived class? In CollectionBase, I can override OnValidate - is there a similar functionality...
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
0
by: Weeble | last post by:
On Jun 20, 11:10 pm, Göran Andersson <gu...@guffa.comwrote: I did it that way because CopyTo has a whole bunch of failure cases to take care of (multi-dimensional array, not enough space,...
6
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class...
4
by: brboLikus | last post by:
Hello! Is it possible to create a List<T> class where T is a random type? This is what I would like to do (or something similar because this doesn't work): Object object = (Object)(new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.