I have a class Friend:

public class Friend 
	{		
			public string friendName;
			public string friendStreet;
			public string friendCity;
			public string friendState;
			public string friendZip;
		
		public Friend()
		{
		}
	}

and I am using a ArrayList called friendArray.
I have a text file that is formatted like this:

My Name
My Street
My City
My State
My Zip
My Name
My Street
My City
My State
My Zip
etc......

So I want to be able to read the text file (all addresses) into the arrayList. But I am having a brain fart. ;)
I am keeping a counter of the records on my GUI so when I add a new address it will increase by one and when I delete it will decrease so I always know how many records are in the ArrayList.
When I try my code it will read the 10 lines and then say that there are 10 records. So its not quite putting the things where they should go.
Also I am also trying to add each FriendName from the arrayList to display in a ListBox, I tried a foreach but did not quite work either.
Any ideas?

private void menuOpen_Click(object sender, System.EventArgs e)
		{
			
			if(File.Exists("AddressList.txt"))
				
			{
				inFile = new StreamReader("AddressList.txt");
				string line = "";
				while((line = inFile.ReadLine())!= null)
				{
					friendArray.Add(line);
				}
			
				
			
				//foreach(Friend fr in friendArray)
				//{
				//	lstAddresses.Items.Add(fr.friendName);

				//}
						
			}
			else
			{
				MessageBox.Show("File not found");
			}

		}

Why do you keep a counter?
ArrayList is so kind of doing that for you : MyArList.Count.

Yes that is what I am using. My point was that I am displaying the count of records on the GUI that is how I know that it reads from the file and stores it into the ArrayList, but when I read the file with 2 addresses then the record count displays 10 so it is reading each line as a new record. I think I need another loop there but I am not sure what exactly to do.

Hello, C#Newbie.
If I were you, I'd probably made some changes in format of the file. E.g. place info about single friend on a single line (using specific separator between the fields, like tabulation symbol, or something like that). So if we'd have 10 friends - it would be 10 lines in a file. Then would make a constructor, that takes a string as parameter, parses it and fills new object with data. And I'd better used List instead of ArrayList. Here is what I'm talking about:

// Same array, but each element would be of Friend type.
List<Friend> friendArray = new List<Friend>();

//adding new friend to an array, and for creating new one, we're using
//line, that we'd read from the file.
friendArray.Add(new Friend(line));

But if you don't like that one :P, here is one more (which is closer to your code):
1. You should add to your ArrayList not 'line', but a new created Friend object.
2. For adding new Friend object, you should fill it by data. You choose how to do that: in constructor, or in some method ...
Doing that way, you'll be able to do this:

foreach(Friend fr in friendArray)

because your friendArray now consists from strings, and not from Strings, as was before.

Hello, C#Newbie.
If I were you, I'd probably made some changes in format of the file. E.g. place info about single friend on a single line (using specific separator between the fields, like tabulation symbol, or something like that). So if we'd have 10 friends - it would be 10 lines in a file. Then would make a constructor, that takes a string as parameter, parses it and fills new object with data. And I'd better used List instead of ArrayList. Here is what I'm talking about:

// Same array, but each element would be of Friend type.
List<Friend> friendArray = new List<Friend>();

//adding new friend to an array, and for creating new one, we're using
//line, that we'd read from the file.
friendArray.Add(new Friend(line));

But if you don't like that one :P, here is one more (which is closer to your code):
1. You should add to your ArrayList not 'line', but a new created Friend object.
2. For adding new Friend object, you should fill it by data. You choose how to do that: in constructor, or in some method ...
Doing that way, you'll be able to do this:

foreach(Friend fr in friendArray)

because your friendArray now consists from strings, and not from Strings, as was before.

Thank you so much I actually got it to work. I tried <List> before but I don't think .Net 1.1 is supporting it. And its for college and we have not learned anything about lists yet. So I had to let that one go. This worked just fine and did the trick. Thanks a lot.

while(inFile.ReadLine() != null)
{
                Friend Fri = new Friend();
                Fri.friendName = inFile.ReadLine();
	        Fri.friendStreet = inFile.ReadLine();
		Fri.friendCity = inFile.ReadLine();
		Fri.friendState = inFile.ReadLine();
		Fri.friendZip = inFile.ReadLine();
		// add the friend to the arrayList
		friendArray.Add(Fri);
		// add the friends name to the listBox
						lstAddresses.Items.Add(Fri.friendName);
This article has been dead for over six months. Start a new discussion instead.