Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 12th, 2007, 09:30 AM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam
Default ArrayList Problem

hey experts
is there any way I can assign an object array to a variable of a specific type in a C# programme?
eg.I defined a class 'Rental' and an object array like this:
private ArrayList _rentals=new ArrayList();
provided that array "_rentals" has some object instances added.
Then I need to assign all those object instances to a variable of a specific type.any way to make it?
It's available in Java programmes:
Enumeration rentals=_rentals.elements();
Rental each=(Rental)rentals.nextElement();
but I'm not sure if it's also available in a c# programme.
great appreciation.kinda urgent!

ERIC
__________________
ERIC
 
Old May 12th, 2007, 09:44 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
foreach (object item in _rentals)
{
  Rental rental = item as Rental;
  //do something with rental
}
You'd be better off using a typed list to start with:
Code:
using System.Collections.Generic;

List<Rental> rentals = new List<Rental>();
Rental rental1 = new Rental();
Rental rental2 = new Rental();
rentals.Add(rental1);
rentals.Add(rental2);
foreach (Rental rental in rentals)
{
  //do something with rental
}
rentals
--

Joe (Microsoft MVP - XML)
 
Old May 12th, 2007, 10:02 AM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam
Default

is this also ok?
eg.IEnumerator rentals = (Rental)_rentals.GetEnumerator();
I can use its method "MoveNext" and property "current" to access each object instance.but how can I know all instances have been accessed?
Jave syntax is like this:
while(rentals.hasMoreElements())
{
  .....
}
how about in a C# programme?THANKS

ERIC
 
Old May 12th, 2007, 10:04 AM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam
Default

oh,I'm so retarded!! foreach(.....),right?

ERIC





Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList Mogg-Way C# 2005 5 February 12th, 2008 04:05 PM
Return an arraylist Morrislgn VB.NET 2002/2003 Basics 1 March 24th, 2006 04:42 AM
Help with an arraylist crazy-nun General .NET 4 July 14th, 2005 03:32 AM
ArrayList kobystud C# 4 May 25th, 2004 02:05 PM
Problem Using ArrayList in VB.Net chiefouko General .NET 0 May 4th, 2004 12:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.