ArrayList and Struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ching-Lung

    ArrayList and Struct

    Hi,

    The code:

    class Row
    {
    public int m_Id;
    public string m_Name;

    public Row(int id, string name)
    {
    m_Id = id;
    m_Name = name;
    }
    }

    class Table
    {
    public ArrayList m_Table;

    public Table()
    {
    Row row1 = new Row(5, "hello");
    Row row2 = new Row(10, "world");

    m_Table = new ArrayList(2);
    m_Table.Add(row );
    m_Table.Add(row 2);
    }

    public int GetId(int rowNumber)
    {
    return m_Table[rowNumber].???
    }

    public string GetName(int rowNumber)
    {
    return m_Table[rowNumber].???
    }
    }

    If I call Table.GetId(0), it should return 5. If I call
    Table.GetName(1 ), it should return "world".

    So, how do I access the public members of class Row from
    the ArrayList in class Table?

    Please help. Thanks!
    -CL
  • Alex Ting

    #2
    Re: ArrayList and Struct

    Hi...

    Try this

    ((Row)m_Table[rowNumber]).Row;
    cast it as a Row and use properties:

    "Ching-Lung" <cltjiong@hotma il.com> wrote in message
    news:008d01c3a4 20$a3eca6d0$a00 1280a@phx.gbl.. .[color=blue]
    > Hi,
    >
    > The code:
    >
    > class Row
    > {
    > public int m_Id;[/color]
    public int M_Id
    {
    get
    {
    return m_Id;
    }
    set
    {
    m_Id = value;
    }
    }[color=blue]
    > public string m_Name;
    >
    > public Row(int id, string name)
    > {
    > m_Id = id;
    > m_Name = name;
    > }
    > }
    >
    > class Table
    > {
    > public ArrayList m_Table;
    >
    > public Table()
    > {
    > Row row1 = new Row(5, "hello");
    > Row row2 = new Row(10, "world");
    >
    > m_Table = new ArrayList(2);
    > m_Table.Add(row );
    > m_Table.Add(row 2);
    > }
    >
    > public int GetId(int rowNumber)
    > {
    > return m_Table[rowNumber].???
    > }
    >
    > public string GetName(int rowNumber)
    > {
    > return m_Table[rowNumber].???
    > }
    > }
    >
    > If I call Table.GetId(0), it should return 5. If I call
    > Table.GetName(1 ), it should return "world".
    >
    > So, how do I access the public members of class Row from
    > the ArrayList in class Table?
    >
    > Please help. Thanks!
    > -CL[/color]


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.536 / Virus Database: 331 - Release Date: 3/11/2003


    Comment

    • Ching-Lung

      #3
      Re: ArrayList and Struct

      Thank you!

      [color=blue]
      >-----Original Message-----
      >Hi...
      >
      >Try this
      >
      >((Row)m_Tabl e[rowNumber]).Row;
      >cast it as a Row and use properties:
      >
      >"Ching-Lung" <cltjiong@hotma il.com> wrote in message
      >news:008d01c3a 420$a3eca6d0$a0 01280a@phx.gbl. ..[color=green]
      >> Hi,
      >>
      >> The code:
      >>
      >> class Row
      >> {
      >> public int m_Id;[/color]
      > public int M_Id
      >{
      > get
      > {
      > return m_Id;
      > }
      > set
      > {
      > m_Id = value;
      > }
      >}[color=green]
      >> public string m_Name;
      >>
      >> public Row(int id, string name)
      >> {
      >> m_Id = id;
      >> m_Name = name;
      >> }
      >> }
      >>
      >> class Table
      >> {
      >> public ArrayList m_Table;
      >>
      >> public Table()
      >> {
      >> Row row1 = new Row(5, "hello");
      >> Row row2 = new Row(10, "world");
      >>
      >> m_Table = new ArrayList(2);
      >> m_Table.Add(row );
      >> m_Table.Add(row 2);
      >> }
      >>
      >> public int GetId(int rowNumber)
      >> {
      >> return m_Table[rowNumber].???
      >> }
      >>
      >> public string GetName(int rowNumber)
      >> {
      >> return m_Table[rowNumber].???
      >> }
      >> }
      >>
      >> If I call Table.GetId(0), it should return 5. If I[/color][/color]
      call[color=blue][color=green]
      >> Table.GetName(1 ), it should return "world".
      >>
      >> So, how do I access the public members of class Row[/color][/color]
      from[color=blue][color=green]
      >> the ArrayList in class Table?
      >>
      >> Please help. Thanks!
      >> -CL[/color]
      >
      >
      >---
      >Outgoing mail is certified Virus Free.
      >Checked by AVG anti-virus system[/color]
      (http://www.grisoft.com).[color=blue]
      >Version: 6.0.536 / Virus Database: 331 - Release Date:[/color]
      3/11/2003[color=blue]
      >
      >
      >.
      >[/color]

      Comment

      Working...