CS0029 C#'不能隐式地将字符串[]转换为字符串'
问题描述:
我正在编写一个应用程序来编辑.mp3文件的属性。我不得不说我对编程和*很新,所以我可能会做一些非常明显的错误。请原谅我!这是我使用的代码:CS0029 C#'不能隐式地将字符串[]转换为字符串'
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();
}
}
奇怪的事情对我来说,我只得到一个错误的这一部分:
if (!string.IsNullOrWhiteSpace(txtTitle.Text))
{
file.Tag.Title = new string[] {txtTitle.Text};
}
有了这个被红色下划线:
new string[] {txtTitle.Text}
我在这里错过了什么?我一直在寻找很长一段时间,但我似乎无法找到任何解决方案。先谢谢你!顺便说一下,我也使用TagLib。
答
更改此:
file.Tag.Title = new string[] {txtTitle.Text};
到:
file.Tag.Title = txtTitle.Text;
Title
类型string
,而不是字符串数组(不string[]
),但你尝试分配阵列 - 因为它,你得到错误。其他字段的类型为string[]
(字符串数组),这就是为什么只有Title
才会出错。
此外,您尝试值赋给Title
2次:
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
您需要分配一次。另外,当您第二次分配时,您可以正确分配,而不会出错。
与Performers
相同的情况 - 您在if
声明中指定的第一次和在最后的if
之后的第二次声明。
答
更改这样的代码,并尝试
file.Tag.Title = txtTitle.Text;
你明白'string'和'字符串[]'之间的区别? –
你正试图把一个'string []'值放入一个'string'变量中。 'file.Tag.Title'是一个'字符串' –
你可以试一下:'file.Tag.Title = txtTitle.Text;' – Hackerman