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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Johnson
    New Member
    • Oct 2010
    • 97

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

    Hi,

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

    Code:
    class fred
    {
      string jim;
      string hilda;
      int eccles;
      bool crunn;
      double vodka;
    }
    Elsewhere in my code I have the following

    Code:
    public void doSomething()
    {
      List<fred> chunky = new List<fred>();
      try
      {
         webservice r = new webservice();
         chunky = r.returnedList;
         // do something
      }
      catch (System.Exception o) 
      {
        fred[0].jim = o.ToString();
        // do something else
      }
    }
    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

    Code:
    chunky = null;
    before I can do something like this? I obviously can't use chunky.Add(o.To String()); 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
  • LittleDong
    New Member
    • Mar 2012
    • 11

    #2
    first, some obvious errors.
    Code:
     string jim;
     string hilda;
     int eccles;
      bool crunn;
      double vodka;
    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!

    Comment

    • LittleDong
      New Member
      • Mar 2012
      • 11

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

      try this and I have tested on my VS

      Code:
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
       
      
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
                  List<fred> chunky = new List<fred>();
                  fred test1 = new fred();
                  test1.jim = "Jim";
                  test1.eccles = 0x01;
                  test1.crunn = true;
      
                  chunky.Add(test1);
              }
      
             
          }
           
                     struct fred
                    {
                    public string jim;
                    public int eccles;
                    public  bool crunn;
                    }
      
      }

      Comment

      Working...