1

I have an arraylist which has a class datastructure.

public class empRec
{
    public string nuView;
    public string firstName;
    public string position;
}

empRec records = new empRec();
...
...

bar.add(records);

now if i want to access the elements i can use

foreach(empRec foo in bar) { ... }

this allows me to print out the values in my class.

I do not know how to make this work using a for loop.

My end goal being to traverse the arraylist and find employee records and change some of the values.

For example, if employeeNum is equal to the previous employeeNum check position id and change the position id.

5
  • If you are using .Net Framework 2.0 or later, you should probably use List<T> instead of the ArrayList to avoid casting. Commented Aug 20, 2009 at 22:25
  • @molocules: did you mean your name to be "molecules", meaning a bunch of atoms put together? Commented Aug 20, 2009 at 22:50
  • Actually it is an ongoing joke. Many moons ago I accidently typed "molocules" as a gamer tag. I have kept it ever since. ahhhhhhh nostalgia. Commented Aug 20, 2009 at 22:52
  • Why the need for a for loop? A foreach should be fine so long as you aren't changing the collection but only the properties of the objects in the collection. Could you provide some code snippets of what you are trying to do in the for loop? Commented Aug 20, 2009 at 22:56
  • empRec foo = bar[i] as empRec; empRec fooo = bar[i - 1] as empRec if (foo.nuView != fooo.nuView) {writer.WriteLine(foo.nuView+", "+foo.firstName+", "+foo.position); } Commented Aug 20, 2009 at 23:27

4 Answers 4

5

I am assuming you really mean ArrayList

for(int i=0; i < bar.Count; i++)
{
   empRec foo = bar[i] as empRec;
}

you should really use List<T> however as it is generic and provides you with type-safety.

List<empRec> bar = new List<empRec>();
bar.Add(yourItem);
.
.
.

for(int i=0; i < bar.Count; i++)
{
   empRec foo = bar[i];
}
Sign up to request clarification or add additional context in comments.

4 Comments

i should really pay more attention the right side of my screen where the formatting reference is. Thanks for fixing it up John.
still doesn't give me the actual values. just says emprec
What do you mean "just says emprec"? Show the code that "just says emprec". I bet you're printing out each foo, and it "just says emprec". If so, you need to override the ToString method on the empRec class.
@JohnSaunders: I don't care for ticks at all, but stomachticks are better than backticks, as they are easier to remove. Of course, Garrapata (the place) is a different story altogether.
3

To make the same thing work using a for loop, you can do the following

for ( int i = 0; i < bar.Count; i++ ) {
  SomeType empRec = (SomeType)(bar[i]);
  ...
}

Replace SomeType with the actual type of the item in the collection.

Comments

0

You should be able to do something akin to the following:

for (int i = 0; i < bar.Count; i++) {
    // Do something with bar[i], bar[i-1], whatever
}

Comments

0

You might consider switching from an ArrayList to a generic List if you can.

This will allow you to specify a type in the List, which not only cleans up your code and allows intellisense to do its job, but it actually has performance benefits too. If the List contains objects of a specified type then you don't have to cast to that type in your loop anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.