copying an arraylist of objects into an array of the objects themselves.

H

hazz

If this makes sense, I would like to convert an arraylist of objects
returned from foo() into an array of the objects themselves per;

private Customer[] customers;
private int idx = 0;

public CustomerQueueService()
{
customers = new Customer[] ;
customers = foo(); //I want to copy the returned arraylist
of Customer(s) into the array of Customer(s)
}

public ArrayList foo() {
ArrayList arr = new ArrayList();
while (dr.Read())
{
Customer oCust = new Customer();

if (!dr.IsDBNull(0)) { oCust.CustomerId = dr.GetInt32(0);}
if (!dr.IsDBNull(1)) { oCust.OtherId = dr.GetInt32(1);}
arr.Add(oCust);
}
return arr;
}

Thank you,
-hazz
 
H

hazz

Thank you Robert!

return (Customer[])arr.ToArray(typeof(Customer));


Robert Jeppesen durius (dot) com> said:
ToArray()?
--
Robert Jeppesen
Durius
http://www.durius.com/

hazz said:
If this makes sense, I would like to convert an arraylist of objects
returned from foo() into an array of the objects themselves per;

private Customer[] customers;
private int idx = 0;

public CustomerQueueService()
{
customers = new Customer[] ;
customers = foo(); //I want to copy the returned
arraylist of Customer(s) into the array of Customer(s)
}

public ArrayList foo() {
ArrayList arr = new ArrayList();
while (dr.Read())
{
Customer oCust = new Customer();

if (!dr.IsDBNull(0)) { oCust.CustomerId =
dr.GetInt32(0);}
if (!dr.IsDBNull(1)) { oCust.OtherId = dr.GetInt32(1);}
arr.Add(oCust);
}
return arr;
}

Thank you,
-hazz
 
H

hazz

Thank you Peter!

return (Customer[])arr.ToArray(typeof(Customer)); is just what I need.


Peter Bromberg said:
Take a look at class ArrayList's CopyTo method.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




hazz said:
If this makes sense, I would like to convert an arraylist of objects
returned from foo() into an array of the objects themselves per;

private Customer[] customers;
private int idx = 0;

public CustomerQueueService()
{
customers = new Customer[] ;
customers = foo(); //I want to copy the returned
arraylist
of Customer(s) into the array of Customer(s)
}

public ArrayList foo() {
ArrayList arr = new ArrayList();
while (dr.Read())
{
Customer oCust = new Customer();

if (!dr.IsDBNull(0)) { oCust.CustomerId =
dr.GetInt32(0);}
if (!dr.IsDBNull(1)) { oCust.OtherId = dr.GetInt32(1);}
arr.Add(oCust);
}
return arr;
}

Thank you,
-hazz
 
G

Guest

I fear we may have lost him in ArrayLand...
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Robert Jeppesen durius (dot) com> said:
ToArray()?
--
Robert Jeppesen
Durius
http://www.durius.com/

hazz said:
If this makes sense, I would like to convert an arraylist of objects
returned from foo() into an array of the objects themselves per;

private Customer[] customers;
private int idx = 0;

public CustomerQueueService()
{
customers = new Customer[] ;
customers = foo(); //I want to copy the returned
arraylist of Customer(s) into the array of Customer(s)
}

public ArrayList foo() {
ArrayList arr = new ArrayList();
while (dr.Read())
{
Customer oCust = new Customer();

if (!dr.IsDBNull(0)) { oCust.CustomerId = dr.GetInt32(0);}
if (!dr.IsDBNull(1)) { oCust.OtherId = dr.GetInt32(1);}
arr.Add(oCust);
}
return arr;
}

Thank you,
-hazz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top