473,434 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

enumerators

hi i have an arraylist that has doctor objects with various properties,
members and methods.

i want to enumerate this arraylist object.
but i dont understand how.
this is what i have done
ArrayList arrDoctors = Doctors.GetAllDoctors();
for (int cnt =0;cnt<1; cnt++)
{
Doctor p = (Doctor) arrDoctors[cnt];
Messagebox.show (p.Name);
//many other properties
}
what is the advantage of enumerators over what i have done?

and how do i implement them for my doctors array
Nov 16 '05 #1
5 3428
The only advantage is that you'll be using a foreach statement.
But you'll still have to cast the Current object to a Doctor.

In v 2.0 of the Framework, the generics are gonna make our lifes much easier
in cases like yours, but for now, you can't use 'em.
Cheers,
Branimir

--
Branimir Giurov
MCSD.NET, MCDBA, MCT
eAgility LLC
"juan" <ic***@hotmail.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
hi i have an arraylist that has doctor objects with various properties,
members and methods.

i want to enumerate this arraylist object.
but i dont understand how.
this is what i have done
ArrayList arrDoctors = Doctors.GetAllDoctors();
for (int cnt =0;cnt<1; cnt++)
{
Doctor p = (Doctor) arrDoctors[cnt];
Messagebox.show (p.Name);
//many other properties
}
what is the advantage of enumerators over what i have done?

and how do i implement them for my doctors array

Nov 16 '05 #2
Hi juan,

foreach (Doctor p in Doctors.GetAllDoctors())
{
Messagebox.show (p.Name);
}

The advantage is more readable code.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"juan" <ic***@hotmail.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
hi i have an arraylist that has doctor objects with various properties,
members and methods.

i want to enumerate this arraylist object.
but i dont understand how.
this is what i have done
ArrayList arrDoctors = Doctors.GetAllDoctors();
for (int cnt =0;cnt<1; cnt++)
{
Doctor p = (Doctor) arrDoctors[cnt];
Messagebox.show (p.Name);
//many other properties
}
what is the advantage of enumerators over what i have done?

and how do i implement them for my doctors array

Nov 16 '05 #3
but dont i have to derive a class from IEnumerable interface?
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:u9*************@TK2MSFTNGP10.phx.gbl...
Hi juan,

foreach (Doctor p in Doctors.GetAllDoctors())
{
Messagebox.show (p.Name);
}

The advantage is more readable code.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"juan" <ic***@hotmail.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
hi i have an arraylist that has doctor objects with various properties,
members and methods.

i want to enumerate this arraylist object.
but i dont understand how.
this is what i have done
ArrayList arrDoctors = Doctors.GetAllDoctors();
for (int cnt =0;cnt<1; cnt++)
{
Doctor p = (Doctor) arrDoctors[cnt];
Messagebox.show (p.Name);
//many other properties
}
what is the advantage of enumerators over what i have done?

and how do i implement them for my doctors array


Nov 16 '05 #4
juan <ic***@hotmail.com> wrote:
but dont i have to derive a class from IEnumerable interface?


Doctors itself doesn't have to - Doctors.GetAllDoctors() returns an
ArrayList, and *that* can be enumerated.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Hi Juan,

You can directly enumerate an "ArrayList" with *foreach* construct.

You will get the advantage of enumerator when you implement
*IEnumerator* and *IEnumerable* interfaces in your "Doctors"
collection class.

In which case you will not be required to populate an extra ArrayList
but can directly enumerate the "Doctors" using *foreach* construct.
public interface IEnumerable {
IEnumerator GetEnumerator();
}

public interface IEnumerator {
Boolean MoveNext();
Object Current { get; }
void Reset();
}

You can check these links for more info:
http://www.codeguru.com/Csharp/Cshar...icle.php/c5505
http://msdn.microsoft.com/library/de...spec_9_3_1.asp
Hope it will help.

--
Cheers,
Rahul Anand
"juan" <ic***@hotmail.com> wrote in message news:<er**************@TK2MSFTNGP09.phx.gbl>...
hi i have an arraylist that has doctor objects with various properties,
members and methods.

i want to enumerate this arraylist object.
but i dont understand how.
this is what i have done
ArrayList arrDoctors = Doctors.GetAllDoctors();
for (int cnt =0;cnt<1; cnt++)
{
Doctor p = (Doctor) arrDoctors[cnt];
Messagebox.show (p.Name);
//many other properties
}
what is the advantage of enumerators over what i have done?

and how do i implement them for my doctors array

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob Shafer | last post by:
Is it possible to create dynamic enumerators in Visual Basic .Net? For example, an enumurator that provides a dropdown of available SQL servers?
6
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally...
1
by: Dave | last post by:
I have several enums that are generated by a code generator (and I have no control over the code generator), the problem is that the names are pretty long enum VeryLoooooooongEnumName {...
2
by: Peter Rilling | last post by:
A design pattern question. As you know, an enumerator in .NET is broken into two interface (IEnumerable and IEnumerator). Is there a benefit in having two interfaces? Why not just have the...
2
by: Andrew Quine | last post by:
Hi Short one: looking for a good example and an explanation of such enumerators. I know MessageEnumerator and ResourceReader do this, for example, but what is the implementation of the...
5
by: juan | last post by:
hi i have an arraylist that has doctor objects with various properties, members and methods. i want to enumerate this arraylist object. but i dont understand how. this is what i have done ...
6
by: Tim Davis | last post by:
I am currently writing a class which I would like to make "enumerable" by inheriting from IEnumerable. The documentation says that the IEnumerator.MoveNext method should throw an exception if the...
1
muaddubby
by: muaddubby | last post by:
Hello all and happy new year. I've seen several posts floating around asking about string enumerators in C#, and generally speaking, they're not supported. I've come up with a way around it...
1
nitindel
by: nitindel | last post by:
Hi All, May i have an in depth article of enumerators in C#..that explains the Enumerators in C# very precisely.... Thanks, Nitin
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.