943,548 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 829
  • C# RSS
Apr 7th, 2009
0

arrays

Expand Post »
Hi
I have a code here composed of Array. I would like to know what does this mean here: (please see the red text)
What does it mean?

Thanks & regards


ArrayList numbers = new ArrayList();
string yourValue;

Console.WriteLine("Give the value (b to end)");
Console.WriteLine();

do
{
Console.WriteLine("Give the value:");
yourValue = Console.ReadLine();

if (yourValue == "b")
break;
numbers.Add(Convert.ToInt32(yourValue));
}
while (true);
System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
while (Enumerator.MoveNext())
Console.WriteLine(" " + Enumerator.Current);
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Apr 7th, 2009
0

Re: arrays

this will loop through all the values in your array
IEnumerator basically means you can use it to iterate your data

lets say you have 1, 5, 3, 8 in your array

this will print out

1 5 3 8
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 7th, 2009
0

Re: arrays

so in looping all the valus I always need this one:
System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
while (Enumerator.MoveNext())
Console.WriteLine(" " + Enumerator.Current);

is that right?

thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Apr 7th, 2009
0

Re: arrays

you don't always have to use that way, but you can

you could also do something like this

C# Syntax (Toggle Plain Text)
  1. foreach(int num in numbers)
  2. {
  3. Console.WriteLine(" " + num);
  4. }
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 7th, 2009
0

Re: arrays

i tried this but when I write the "b" to end, I got an error. this is what I've done.


C# Syntax (Toggle Plain Text)
  1. while (true);
  2. //this will loop through all the values in your array IEnumerator basically means you can use it to
  3. //iterate your data
  4. foreach (int num in numbers)
  5. {
  6. Console.WriteLine(" " + num);
  7. }
  8.  
  9.  
  10.  
  11. //System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
  12. //while (Enumerator.MoveNext())
  13. // Console.WriteLine(" " + Enumerator.Current);
  14. }
Last edited by tintincute; Apr 7th, 2009 at 5:11 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Apr 7th, 2009
0

Re: arrays

its because you are using an untyped list for one, but you didn't have an else statement

C# Syntax (Toggle Plain Text)
  1. if (yourValue == "b")
  2. {
  3. break;
  4. }
  5. else
  6. {
  7. numbers.Add(Convert.ToInt32(yourValue));
  8. }

also you could use List instead of ArrayList

C# Syntax (Toggle Plain Text)
  1. List<int> numbers = new List<int>();
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 9th, 2009
0

Re: arrays


also you could use List instead of ArrayList

C# Syntax (Toggle Plain Text)
  1. List<int> numbers = new List<int>();

You don't really need the typed List for what's going on, it's just a nice thing to have in terms of type safety...
Last edited by alc6379; Apr 9th, 2009 at 11:08 pm.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Apr 11th, 2009
0

Re: arrays

ok i'll try that again and will publish the results.
Thanks;-)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009
Apr 13th, 2009
0

Re: arrays

hi
thanks! it works-)
also the List<> i tried and it also works
have a good day;-)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
tintincute is offline Offline
55 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Run DLL as a App?
Next Thread in C# Forum Timeline: how to connect between 2 computers in C# ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC