"Cannot implicitly convert string to string []"

lynxAi

Member
Joined
Dec 7, 2019
Messages
15
Location
SiChuan China
Programming Experience
Beginner
C#:
    public partial class Form1 : Form
    {
        string[] paths, files; // Define two arrays.

        public Form1()
        {
        }
        private void OpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;
            if(dialog .ShowDialog ()==System .Windows .Forms .DialogResult .OK )
            {
                files = dialog.SafeFileName;   //error with“Cannot implicitly convert string to string []”
                paths = dialog.FileName;      //error as same as above.
                for(int i=0;i<files.Length ;i++)
                {
                    PlayList.Items.Add(files[i]);
                }
            }
        }
 
OpenFileDialog.SafeFileName is a single string. On line 3, you declared files to be a string array. As the error says the compiler can't convert a single string into an array of strings. The same issue for OpenFileDialog.FileName and paths.

To fix your problem, you'll want to access OpenFileDialog.SafeFileNames and OpenFileDialog.FileNames which return arrays.
 
Last edited by a moderator:
OpenFileDialog.SafeFileName is a single string. On line 3, you declared files to be a string array. As the error says the compiler can't convert a single string into an array of strings. The same issue for OpenFileDialog.FileName and paths.

To fix your problem, you'll want to access OpenFileDialog.SafeFileNames and OpenFileDialog.FileNames which return arrays.

This is the code I saw in the instructional video. I haven’t changed it at all, but he didn’t make any mistakes. I have it. The .net version is also the same. Will it be other reasons. Can you say simpler and clearer? Thank you
 
Post the link to the video. But of that is really the code they presented, then the video is wrong. I suggest learning elsewhere.

There is no simpler way to say it. C# is a strictly typed language (unless you use dynamics). The types need to match, or there has to be some type conversion. There is no automatic type conversion from a string to an array of strings.
 
Post the link to the video. But of that is really the code they presented, then the video is wrong. I suggest learning elsewhere.

There is no simpler way to say it. C# is a strictly typed language (unless you use dynamics). The types need to match, or there has to be some type conversion. There is no automatic type conversion from a string to an array of strings.
Can you help me correct this mistake? I will send you a document. It will delay you for 2 minutes. Ok? I will find your changes and then understand the solution
 

Attachments

  • MediaPlayer demo.zip
    19.9 KB · Views: 20
That's the code, but where is the link to the video?
 
Look closely at the video. He is using SafeFileNames and FileNames:
Screenshot_1.png




Jump to 2:14.
 
Oh o_O, there is only one "s" missing. The typing system automatically recommends these two words. I was wrong. Thank you for your help, in fact your first reply has already been answered.
 
Back
Top Bottom