473,320 Members | 1,707 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,320 software developers and data experts.

Having a problem with a list<> of objects - any ideas?

Paul Johnson
Hi,

I have a class which looks like this (the class is then used to read back from a webservice)

Expand|Select|Wrap|Line Numbers
  1. class fred
  2. {
  3.   string jim;
  4.   string hilda;
  5.   int eccles;
  6.   bool crunn;
  7.   double vodka;
  8. }
  9.  
Elsewhere in my code I have the following

Expand|Select|Wrap|Line Numbers
  1. public void doSomething()
  2. {
  3.   List<fred> chunky = new List<fred>();
  4.   try
  5.   {
  6.      webservice r = new webservice();
  7.      chunky = r.returnedList;
  8.      // do something
  9.   }
  10.   catch (System.Exception o) 
  11.   {
  12.     fred[0].jim = o.ToString();
  13.     // do something else
  14.   }
  15. }
  16.  
Problem is coming from the catch. AIUI, what I've done is correct as I've instantated a new list. OK, it doesn't contain anything at the top, but the way I've addressed the list should be correct - but it dies the code dies at the adding of the line.

Do I need to have a line at the top of the code that says

Expand|Select|Wrap|Line Numbers
  1. chunky = null;
before I can do something like this? I obviously can't use chunky.Add(o.ToString()); here as the poor compiler won't know what in the generic list has to have the string added to it...

Any help here would be appreciated.

Paul
Mar 26 '12 #1

✓ answered by LittleDong

FYI: struct is enough for you to orangnize your data.

try this and I have tested on my VS

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.  
  13.  
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.             List<fred> chunky = new List<fred>();
  20.             fred test1 = new fred();
  21.             test1.jim = "Jim";
  22.             test1.eccles = 0x01;
  23.             test1.crunn = true;
  24.  
  25.             chunky.Add(test1);
  26.         }
  27.  
  28.  
  29.     }
  30.  
  31.                struct fred
  32.               {
  33.               public string jim;
  34.               public int eccles;
  35.               public  bool crunn;
  36.               }
  37.  
  38. }
  39.  

2 1839
first, some obvious errors.
Expand|Select|Wrap|Line Numbers
  1.  string jim;
  2.  string hilda;
  3.  int eccles;
  4.   bool crunn;
  5.   double vodka;
  6.  
  7.  
these varibles' accessing authority is private. so fred's object can't access them.

second, when you use chunky.Add( ),
the transferred parameter shouble be fred's object.

does this return a list of fred: r.returnedList?

i'm not native speaker, hope make myself clear!
Mar 27 '12 #2
FYI: struct is enough for you to orangnize your data.

try this and I have tested on my VS

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.  
  13.  
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.             List<fred> chunky = new List<fred>();
  20.             fred test1 = new fred();
  21.             test1.jim = "Jim";
  22.             test1.eccles = 0x01;
  23.             test1.crunn = true;
  24.  
  25.             chunky.Add(test1);
  26.         }
  27.  
  28.  
  29.     }
  30.  
  31.                struct fred
  32.               {
  33.               public string jim;
  34.               public int eccles;
  35.               public  bool crunn;
  36.               }
  37.  
  38. }
  39.  
Mar 27 '12 #3

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

Similar topics

4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
3
by: Eric | last post by:
I have a string representation of an object. I create an object of that type through reflection. I would like to create a List<> of those objects. I obviously can't do List<myObject.GetType()>...
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
4
by: =?Utf-8?B?TGFycnlS?= | last post by:
I need some help with a multilevel sorting problem with the List<>. I have a List<ItemToSort( see below ) that needs to be sorted in the following manner: Sort by Level1Id ( ok that was the easy...
0
by: submicron | last post by:
Hi All, I'm having problems implementing a readonly interface to a set of classes. Heres a simplification of the code: interface IreadonlyBase { // Does gets only on base class } class...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
4
by: Peted | last post by:
I have the following code public enum pdfFlags { PFD_DRAW_TO_WINDOW, PFD_DRAW_TO_BITMAP, PFD_SUPPORT_GDI, PFD_SUPPORT_OPENGL, PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
9
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've an optimization question for you all really quick. I have a stream that I am reading some bytes. At times, the stream can contain a small amount of bytes such as 50...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.