Accessing values in custom class stored in ArrayList


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Accessing values in custom class stored in ArrayList

  1. #1
    Jason Salas Guest

    Accessing values in custom class stored in ArrayList

    Hi everyone,

    How would I access the properties of a custom class I've developed once
    instances of that class are added within a .NET collection, specifically an
    ArrayList?

    Here's what I'm doing:

    public class Order
    {
    private string fname,lname;
    public string FName { get { return fname; } }
    public string LName { get { return lname; } }

    public Order(string first,string last)
    {
    this.fname = first;
    this.lname = last;
    }
    }

    ArrayList arr = new ArrayList();
    arr.Add(new Order("Jason","Salas"));
    arr.Add(new Order("Bart","Simpson"));
    arr.Add(new Order("Andrew","Dice Clay"));

    My question is how would I programmatically get a refernence to one of the
    Order objects in arr, so that I access and use its propety values?

    I'm not sure this would work:

    for(i=0;i<arr.Length-1;i++)
    {
    (Order)arr[i].FName;
    }

    Got any ideas?

    Thanks!
    Jas



  2. #2
    Russell Jones Guest

    Re: Accessing values in custom class stored in ArrayList

    The ArrayList returns a generic Object reference. Your code would try to
    cast the return value of the Object.FName method to an Order. You just need
    some more parentheses . Also, the ArrayList isn't an Array, and doesn't
    have a Length method. It's a collection type, and has a Count method
    instead. For example

    for(int i=0;i<arr.Count-1;i++)
    {
    Console.Out.WriteLine(((Order)arr[i]).FName);
    }

    "Jason Salas" <jason@kuam.com> wrote in message
    news:3f27a665$1@tnews.web.devx.com...
    > Hi everyone,
    >
    > How would I access the properties of a custom class I've developed once
    > instances of that class are added within a .NET collection, specifically

    an
    > ArrayList?
    >
    > Here's what I'm doing:
    >
    > public class Order
    > {
    > private string fname,lname;
    > public string FName { get { return fname; } }
    > public string LName { get { return lname; } }
    >
    > public Order(string first,string last)
    > {
    > this.fname = first;
    > this.lname = last;
    > }
    > }
    >
    > ArrayList arr = new ArrayList();
    > arr.Add(new Order("Jason","Salas"));
    > arr.Add(new Order("Bart","Simpson"));
    > arr.Add(new Order("Andrew","Dice Clay"));
    >
    > My question is how would I programmatically get a refernence to one of the
    > Order objects in arr, so that I access and use its propety values?
    >
    > I'm not sure this would work:
    >
    > for(i=0;i<arr.Length-1;i++)
    > {
    > (Order)arr[i].FName;
    > }
    >
    > Got any ideas?
    >
    > Thanks!
    > Jas
    >
    >




  3. #3
    Jason Salas Guest

    Re: Accessing values in custom class stored in ArrayList

    Oops....thanks again Russ!


    "Russell Jones" <arj1@nospam.northstate.net> wrote in message
    news:3f27d9ca$1@tnews.web.devx.com...
    > The ArrayList returns a generic Object reference. Your code would try to
    > cast the return value of the Object.FName method to an Order. You just

    need
    > some more parentheses . Also, the ArrayList isn't an Array, and doesn't
    > have a Length method. It's a collection type, and has a Count method
    > instead. For example
    >
    > for(int i=0;i<arr.Count-1;i++)
    > {
    > Console.Out.WriteLine(((Order)arr[i]).FName);
    > }
    >
    > "Jason Salas" <jason@kuam.com> wrote in message
    > news:3f27a665$1@tnews.web.devx.com...
    > > Hi everyone,
    > >
    > > How would I access the properties of a custom class I've developed once
    > > instances of that class are added within a .NET collection, specifically

    > an
    > > ArrayList?
    > >
    > > Here's what I'm doing:
    > >
    > > public class Order
    > > {
    > > private string fname,lname;
    > > public string FName { get { return fname; } }
    > > public string LName { get { return lname; } }
    > >
    > > public Order(string first,string last)
    > > {
    > > this.fname = first;
    > > this.lname = last;
    > > }
    > > }
    > >
    > > ArrayList arr = new ArrayList();
    > > arr.Add(new Order("Jason","Salas"));
    > > arr.Add(new Order("Bart","Simpson"));
    > > arr.Add(new Order("Andrew","Dice Clay"));
    > >
    > > My question is how would I programmatically get a refernence to one of

    the
    > > Order objects in arr, so that I access and use its propety values?
    > >
    > > I'm not sure this would work:
    > >
    > > for(i=0;i<arr.Length-1;i++)
    > > {
    > > (Order)arr[i].FName;
    > > }
    > >
    > > Got any ideas?
    > >
    > > Thanks!
    > > Jas
    > >
    > >

    >
    >




  4. #4
    Ed Courtenay Guest

    Re: Accessing values in custom class stored in ArrayList

    Of course you might find that it's simply easier to iterate over the the
    array using foreach:

    using System;
    using System.Collections;

    public class Order
    {
    private string fname,lname;
    public string FName { get { return fname; } }
    public string LName { get { return lname; } }

    public Order(string first,string last)
    {
    this.fname = first;
    this.lname = last;
    }
    }


    public class Test
    {
    public static void Main() {
    ArrayList arr = new ArrayList();
    arr.Add(new Order("Jason","Salas"));
    arr.Add(new Order("Bart","Simpson"));
    arr.Add(new Order("Andrew","Dice Clay"));


    foreach (Order order in arr) {
    Console.WriteLine(String.Format("{0} {1}", new object[] {
    order.FName,
    order.LName
    }));
    }
    }
    }


    "Russell Jones" <arj1@nospam.northstate.net> wrote in message
    news:3f27d9ca$1@tnews.web.devx.com...
    > The ArrayList returns a generic Object reference. Your code would try to
    > cast the return value of the Object.FName method to an Order. You just

    need
    > some more parentheses . Also, the ArrayList isn't an Array, and doesn't
    > have a Length method. It's a collection type, and has a Count method
    > instead. For example
    >
    > for(int i=0;i<arr.Count-1;i++)
    > {
    > Console.Out.WriteLine(((Order)arr[i]).FName);
    > }
    >
    > "Jason Salas" <jason@kuam.com> wrote in message
    > news:3f27a665$1@tnews.web.devx.com...
    > > Hi everyone,
    > >
    > > How would I access the properties of a custom class I've developed once
    > > instances of that class are added within a .NET collection, specifically

    > an
    > > ArrayList?
    > >
    > > Here's what I'm doing:
    > >
    > > public class Order
    > > {
    > > private string fname,lname;
    > > public string FName { get { return fname; } }
    > > public string LName { get { return lname; } }
    > >
    > > public Order(string first,string last)
    > > {
    > > this.fname = first;
    > > this.lname = last;
    > > }
    > > }
    > >
    > > ArrayList arr = new ArrayList();
    > > arr.Add(new Order("Jason","Salas"));
    > > arr.Add(new Order("Bart","Simpson"));
    > > arr.Add(new Order("Andrew","Dice Clay"));
    > >
    > > My question is how would I programmatically get a refernence to one of

    the
    > > Order objects in arr, so that I access and use its propety values?
    > >
    > > I'm not sure this would work:
    > >
    > > for(i=0;i<arr.Length-1;i++)
    > > {
    > > (Order)arr[i].FName;
    > > }
    > >
    > > Got any ideas?
    > >
    > > Thanks!
    > > Jas
    > >
    > >

    >
    >




  5. #5
    Jason Salas Guest

    Re: Accessing values in custom class stored in ArrayList

    Hi Ed,

    Thanks for the help. There's a discussion going on about this right now in
    the ASPAdvice mailing lists, where the theory seems to be upon examination
    of the resultant IL, foreach... takes up to 50-200% longer to execute than a
    for.. loop. Pretty interesting stuff. I can send you the note that
    documented this, if you like.

    Jas



    "Ed Courtenay" <replace-this-with-my-first-name@edcourtenay.co.uk> wrote in
    message news:3f28d6f2$1@tnews.web.devx.com...
    > Of course you might find that it's simply easier to iterate over the the
    > array using foreach:
    >
    > using System;
    > using System.Collections;
    >
    > public class Order
    > {
    > private string fname,lname;
    > public string FName { get { return fname; } }
    > public string LName { get { return lname; } }
    >
    > public Order(string first,string last)
    > {
    > this.fname = first;
    > this.lname = last;
    > }
    > }
    >
    >
    > public class Test
    > {
    > public static void Main() {
    > ArrayList arr = new ArrayList();
    > arr.Add(new Order("Jason","Salas"));
    > arr.Add(new Order("Bart","Simpson"));
    > arr.Add(new Order("Andrew","Dice Clay"));
    >
    >
    > foreach (Order order in arr) {
    > Console.WriteLine(String.Format("{0} {1}", new object[] {
    > order.FName,
    > order.LName
    > }));
    > }
    > }
    > }
    >
    >
    > "Russell Jones" <arj1@nospam.northstate.net> wrote in message
    > news:3f27d9ca$1@tnews.web.devx.com...
    > > The ArrayList returns a generic Object reference. Your code would try to
    > > cast the return value of the Object.FName method to an Order. You just

    > need
    > > some more parentheses . Also, the ArrayList isn't an Array, and

    doesn't
    > > have a Length method. It's a collection type, and has a Count method
    > > instead. For example
    > >
    > > for(int i=0;i<arr.Count-1;i++)
    > > {
    > > Console.Out.WriteLine(((Order)arr[i]).FName);
    > > }
    > >
    > > "Jason Salas" <jason@kuam.com> wrote in message
    > > news:3f27a665$1@tnews.web.devx.com...
    > > > Hi everyone,
    > > >
    > > > How would I access the properties of a custom class I've developed

    once
    > > > instances of that class are added within a .NET collection,

    specifically
    > > an
    > > > ArrayList?
    > > >
    > > > Here's what I'm doing:
    > > >
    > > > public class Order
    > > > {
    > > > private string fname,lname;
    > > > public string FName { get { return fname; } }
    > > > public string LName { get { return lname; } }
    > > >
    > > > public Order(string first,string last)
    > > > {
    > > > this.fname = first;
    > > > this.lname = last;
    > > > }
    > > > }
    > > >
    > > > ArrayList arr = new ArrayList();
    > > > arr.Add(new Order("Jason","Salas"));
    > > > arr.Add(new Order("Bart","Simpson"));
    > > > arr.Add(new Order("Andrew","Dice Clay"));
    > > >
    > > > My question is how would I programmatically get a refernence to one of

    > the
    > > > Order objects in arr, so that I access and use its propety values?
    > > >
    > > > I'm not sure this would work:
    > > >
    > > > for(i=0;i<arr.Length-1;i++)
    > > > {
    > > > (Order)arr[i].FName;
    > > > }
    > > >
    > > > Got any ideas?
    > > >
    > > > Thanks!
    > > > Jas
    > > >
    > > >

    > >
    > >

    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center