473,396 Members | 1,760 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,396 software developers and data experts.

Index of object array

Say I am iterating through an objects values:

int i;
DirectoryInfo di = new DirectoryInfo("c:/inetpub/wwwroot/demos");
FileInfo[] rgFiles = di.GetFiles("*.aspx");
foreach(FileInfo fi in rgFiles)
htmlPgs[i++]=fi.Name;

What property of rgFiles could be substituted for i?
That is, can I write:
htmlPgs[rgFiles.<enumerator>];
instead?
How?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.752 / Virus Database: 503 - Release Date: 9/3/2004
Nov 16 '05 #1
4 2020
"Spare Change" <sp*********@spam.spam> wrote in
news:xP***************@newsread3.news.pas.earthlin k.net...
Say I am iterating through an objects values:

int i;
DirectoryInfo di = new DirectoryInfo("c:/inetpub/wwwroot/demos");
FileInfo[] rgFiles = di.GetFiles("*.aspx");
foreach(FileInfo fi in rgFiles)
htmlPgs[i++]=fi.Name;

What property of rgFiles could be substituted for i?
That is, can I write:
htmlPgs[rgFiles.<enumerator>];
instead?


No: that's the main disadvantage of a foreach loop;

Internally the foreach loop queries the collection object for the
IEnumerable interface, gets an IEnumerator object from it, which only
exposes the methods "Current", "MoveNext", and "Reset": There's no way to
get an index from it, unless the object in question has some special
provisions for that (I know none that would).

I guess the main reason for this architecture is to ensure the foreach loop
can be used on any kind of collection: If you're iterating over a linked
list, database query keeping an index would be quite complex, as the
collection may change while it's iterated.

Niki
Nov 16 '05 #2
"Spare Change" <sp*********@spam.spam> wrote:
foreach(FileInfo fi in rgFiles)
htmlPgs[i++]=fi.Name;
What property of rgFiles could be substituted for i?
That is, can I write:
htmlPgs[rgFiles.<enumerator>];
instead?


When you need an index variable, use a regular for loop based on
Length or Count.

for (int i = 0; i < rgFiles.Length; i++)
{
// do something - this is the ith iteration
}

P.
Nov 16 '05 #3

I understand. But if I want to use the object type of the array, like the
FileInfo type, in the inside code. It just seems like a foreach is doing
some kind of enumeration, there must be a "selected" property, or
something, for the collection....
Paul E Collins wrote:
"Spare Change" <sp*********@spam.spam> wrote:
foreach(FileInfo fi in rgFiles)
htmlPgs[i++]=fi.Name;
What property of rgFiles could be substituted for i?
That is, can I write:
htmlPgs[rgFiles.<enumerator>];
instead?


When you need an index variable, use a regular for loop based on
Length or Count.

for (int i = 0; i < rgFiles.Length; i++)
{
// do something - this is the ith iteration
}

P.


--
incognito http://kentpsychedelic.blogspot.com
new material added 9/5
Nov 16 '05 #4
"The Devil" <el******@hadez.nyc.spamo> wrote in
news:QD*****************@newsread1.news.pas.earthl ink.net...

I understand. But if I want to use the object type of the array, like the
FileInfo type, in the inside code. It just seems like a foreach is doing
some kind of enumeration, there must be a "selected" property, or
something, for the collection....


If that was the case, you couldn't write code like this:

foreach (int x in samples)
foreach (int y in samples)
z += x*y;

which is of course perfectly legal.

Niki
Nov 16 '05 #5

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

Similar topics

4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
0
by: scotthutchinson | last post by:
I have a .NET Remoting object hosted in IIS6 on Windows Server 2003 (happens before and after installing SP1) at an endpoint (ASP.NET application virtual folder) named "CompanyXYZReporting". The...
3
by: John Dolan | last post by:
Hi all, I need some help here. I have an ActiveX object that requires me to pass in an array that is 1-based (as opposed to the normal 0-based index). Unfortunately the author of the ActiveX...
4
by: Antoine | last post by:
Herfried and Cor:- I used tracing and actually tracked down the code that was causing the problem most likely. I wonder if you wanted to comment on it. Also I wonder if there is a better way...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
6
by: routeslip | last post by:
I'm refering to an entry in an array by it's string key, as in foo. Is there a way to get the numeric index of that array without iterating through the entire array? What I need to do is find...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
8
by: Joe Rattz | last post by:
Ok, I can't believe what I am seeing. I am sure I do this other places with no problems, but I sure can't here. I have some code that is indexing into the ItemArray in a DataSet's DataRow. I...
7
by: d d | last post by:
I have an array of objects that start out looking like this: var ra=; I want to be able to access the array by index number from some code, and by the name property from other code, as if it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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,...

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.