CS0029 C# 'Cannot implicitly convert type string[] to string'

12,291

Solution 1

Change this:

file.Tag.Title = new string[] {txtTitle.Text};

to:

file.Tag.Title = txtTitle.Text;

Title type is string, not array of strings (not string[]), but you try to assign array - because of it you get error. Other fields have type string[] (array of strings), that's why you get error only with Title.

Also, you try to assign value to Title 2 times:

if (!string.IsNullOrWhiteSpace(txtTitle.Text))
{
    file.Tag.Title = new string[] {txtTitle.Text};    // first time
}
file.Tag.Performers = new string[] { txtArtist.Text };
file.Tag.Title = txtTitle.Text;                       //second time

You need to assign only one time. Also, when you assign second time you assign it correctly without error.

The same situation with Performers - you assign first time inside if statement and second time after the last if.

Solution 2

Change the code like this and try

file.Tag.Title = txtTitle.Text;
Share:
12,291
Luctia
Author by

Luctia

23 y/o computer science student from the Netherlands.

Updated on July 30, 2022

Comments

  • Luctia
    Luctia almost 2 years

    I'm making an application to edit the properties of .mp3 files. I have to say I'm pretty new to programming and stackoverflow, so I might be doing something very obviously wrong. Please forgive me! This is the code I am using:

    private void btnApply_Click(object sender, EventArgs e)
    {
        var file = TagLib.File.Create(filepath);
        if (!string.IsNullOrWhiteSpace(txtGenre.Text))
        {
            file.Tag.Genres = new string[] {txtGenre.Text};
        }
        if (!string.IsNullOrWhiteSpace(txtArtist.Text))
        {
            file.Tag.Performers = new string[] {txtArtist.Text};
        }
        if (!string.IsNullOrWhiteSpace(txtTitle.Text))
        {
            file.Tag.Title = new string[] {txtTitle.Text};
        }
        file.Tag.Performers = new string[] { txtArtist.Text };
        file.Tag.Title = txtTitle.Text;
        file.Save();
    
        if (!ReadFile())
        {
            Close();
        }
    }
    

    The odd thing to me is that I only get an error for this part:

    if (!string.IsNullOrWhiteSpace(txtTitle.Text))
    {
        file.Tag.Title = new string[] {txtTitle.Text};
    }
    

    With this being underlined red:

    new string[] {txtTitle.Text}
    

    What am I missing here? I've been looking for a very long time but I just can't seem to find any solutions. Thank you in advance! I am also using TagLib, by the way.