1

Why does it complain about it not being able to convert a string array to a string when the values are strings within a string array

Code:

 int i;
            string[] Filenames;

            OpenFileDialog UnConvertedFilesList = new OpenFileDialog();
            if (UnConvertedFilesList.ShowDialog() == DialogResult.OK)
            {

                foreach (string FileName in UnConvertedFilesList.FileNames)
                {

                    //Right Here
                    Filenames[i] = Filenames;
                    AudioFiles_listbox.Items.Add(FileName);
                    i++;
                }

            }//if
            else
            {
                MessageBox.Show("File does not exist");
            }

edit: that line to Changed Filenames[i] = FileName

Now it says "Use of unassigned local variable 'Filenames' and same thing for i

Their defined at the top of the function.

2 Answers 2

6

You have an extra "s" on your name there:

//Right Here
Filenames[i] = Filenames;

should be:

//Right Here
Filenames[i] = FileName;

More than that, though, your Filenames[] array is currently null. Once you fix your first problem you'll have that to contend with. My recommendation there is to skip using the array entirely and just go directly to the AudioFiles_listbox. And once you do that, you can go right for the Listbox's AddRange method:

AudioFiles_listbox.Items.AddRange(UnConvertedFilesList.FileNames);
Sign up to request clarification or add additional context in comments.

3 Comments

It's actually FileName - note the capital N. But he should figure that out. Also i is never initialised. He needs int i=0. Cheers +1
@JoelCoehoom, please provide the solution code for doing this alternative approach you described.
Yeah for "string[] FileNames;" I get the error: "The variable 'FileNames is declared but never used" so if i leave off that line it will still work? I have a problem w/ the window loading twice and not allowing me to select more than one file.
0

"Use of unassigned" means that you aren't using it anywhere within that method or segment of code... your assignment is now OK, but it's not being used.

HTH.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.