Hi guys, as the title suggest, Im new to c#. I have a text file with 5 lines of text in it. Im trying to read each line and throw it into an array of string type.
But Im having problems.

private void button4_Click(object sender, EventArgs e)
        {
            songs = new string[5];

            OpenFileDialog grabSong = new OpenFileDialog();

            if (grabSong.ShowDialog() == DialogResult.OK)
            {
                playlist = grabSong.FileName; //playlist is just a private member of string type.

                TextReader tr = new StreamReader(playlist);

                for (int i = 0; i != 5; i++)
                {
                    songs[i] = tr.ReadLine();
                }

                tr.Close();                
                
            }            

        }

Edit: I debugged it when I first tryed to check if the array was being filled properly, and it wasnt. Just debugged again and it is working fine now. Weird =p.

But Ill still post this if anyone can give some better suggestions on how to do this. For instance, in my for conditions, I cant say, (int i = 0; i != songs.count; i++) like I would be able to in c++.

Any tips? Thanks.

For instance, in my for conditions, I cant say, (int i = 0; i != songs.count; i++) like I would be able to in c++.

For that one I would say this instead:

for (int i = 0; i < songs.Count; i++)

Since Count is based on numbers starting at 1 and the array is based on numbers starting from 0 this will effectively stop the FOR loop prior to going out of bounds.

EDIT: Just thought of this but it may be songs.Length instead of songs.Count... hmm... brain not working today, not sure.

Otherwise, your method appears sound.

Hope this helps :)

Edited 6 Years Ago by Lusiphur: n/a

ah, using i != songs.Count would give error, but I never tried i < songs.count. That seems alittle more dynamic and useful to me. Thanks for the tidbit =)

Wonder how you could compile this:

songs = new string[5];

Is the variable songs defined elsewhere?
I should say this has to be:

string[] songs = new string[5];

You should use File.ReadAllLines(), it will make it shorter and will work with however many lines you put into the file:

songs = File.ReadAllLines(playlist);

You should use File.ReadAllLines(), it will make it shorter and will work with however many lines you put into the file:

songs = File.ReadAllLines(playlist);

He's reading line-by-line in order to itemize the songs in an array. How will reading all lines at once without separating them help him?

Comments
Could not have said better :)

He's reading line-by-line in order to itemize the songs in an array. How will reading all lines at once without separating them help him?

Did you even bother to read the documentation on ReadAllLines? It returns an array of strings, not one big continuous string (For that you would use ReadAllText). Each line is already separated for you. I expected better from someone with your reputation.

Edited 6 Years Ago by Momerath: n/a

Comments
Bravo, you caught me in a mistake and gave me my first down-vote... kudos to you :twisted:

*shrug* Everyone has their off moments.

The fact remains that his method is perfectly sound and can be used as it stands. The fact further remains that your method also works and could be used in it's stead.

What doesn't make much sense to me, however, is your feeling that you need to personally attack me for a very minor mistake :twisted: And, for the record, there are some benefits to looping a line-by-line read in many situations over a full-file dump. While this situation would work just as well either way, not all situations would.

My apologies, your holiness, for misreading your suggestion and reading it as the wrong method... I will endeavour to do better in the future.

This article has been dead for over six months. Start a new discussion instead.