简体   繁体   English

我收到一个错误:使用未分配的局部变量“艺术家”,不知道我在做什么错?

[英]I get an error: use of unassigned local variable 'artist', don't know what i'm doing wrong?

private void btn_AddSongArtistPlaylist_Click(object sender, EventArgs e)
{
        Artist artist;

        if (cmbBox_AddSongPlaylistArtist.SelectedIndex == 0)
        {
            if (cmbBox_Artist.SelectedIndex == 0)
            {
                artist = new Artist(txtBox_NameArtist.Text, dateTP_BirthdatArtist.Value.Date);
            }
            else 
            {
                foreach (Artist a in Artists)
                {
                    if (a.Name == cmbBox_Artist.SelectedText)
                    {
                        artist = a;
                    }
                }
            }
            Song song = new Song(txtBox_Name.Text, Convert.ToInt32(txtBox_YearBirthday.Text), artist, txtBox_PathToFile.Text);
        }
    }

You are using the potentially unassigned variable artist . 您正在使用可能未分配的变量artist See: 看到:

private void btn_AddSongArtistPlaylist_Click(object sender, EventArgs e)
    {
        Artist artist;
        if (cmbBox_AddSongPlaylistArtist.SelectedIndex == 0)
        {
            if (cmbBox_Artist.SelectedIndex == 0)
            {
                artist = new Artist(txtBox_NameArtist.Text, dateTP_BirthdatArtist.Value.Date);
            }
            else 
            {
                foreach (Artist a in Artists)
                {
                    if (a.Name == cmbBox_Artist.SelectedText)
                    {
                        artist = a;
                    }
                }
            }
            Song song = new Song(txtBox_Name.Text, Convert.ToInt32(txtBox_YearBirthday.Text), artist, txtBox_PathToFile.Text); // right here!
        }
    }

If the second if statement evaluates to false , and there are no objects in the Artists collection, you will hit that new Song line with an unassigned variable. 如果第二个if语句的计算结果为false ,并且Artists集合中没有对象,则将使用未分配的变量命中new Song行。 Try initializing artist to null . 尝试将artist初始化为null

private void btn_AddSongArtistPlaylist_Click(object sender, EventArgs e)
    {
        Artist artist = null;
        if (cmbBox_AddSongPlaylistArtist.SelectedIndex == 0)
        {
            if (cmbBox_Artist.SelectedIndex == 0)
            {
                artist = new Artist(txtBox_NameArtist.Text, dateTP_BirthdatArtist.Value.Date);
            }
            else 
            {
                foreach (Artist a in Artists)
                {
                    if (a.Name == cmbBox_Artist.SelectedText)
                    {
                        artist = a;
                    }
                }
            }
            Song song = new Song(txtBox_Name.Text, Convert.ToInt32(txtBox_YearBirthday.Text), artist, txtBox_PathToFile.Text); // now you're fine
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能知道我出了什么问题,因为我是统一错误 CS0165 的新手:使用未分配的局部变量 'diff' - can anyone what I'm going wrong as I'm new in unity error CS0165: Use of unassigned local variable 'diff' 货币转换器使用未分配的局部变量错误? 抱歉 - Use of unassigned local variable error for currency converter? I'm Sorry 我不知道我的 C# 代码做错了什么 - I don't know what I'm doing wrong in my C# code 不知道我在单元测试中做错了什么 - Don't know what I'm doing wrong in my Unit Test 为什么会出现错误“使用未分配的局部变量”? - Why do I get the error “Use of Unassigned local Variable”? 为什么我收到编译错误“使用未分配的局部变量”? - Why did I get the compile error "Use of unassigned local variable"? 我正在验证 C# 中的表格,但我不知道我做错了什么。 请帮我解决这个问题 - I'm validating a form in C# but I don't know what I am doing wrong. Please help me solve this 编译器错误-“使用未分配的局部变量”。 有什么问题吗? - Compiler error - “use of unassigned local variable”. what can go wrong? 使用未分配的局部变量 - 但我知道,当程序到达它时,它将被分配 - Use of unassigned local variable - but I know that by the time the program reaches it, it will be assigned 即使我分配了变量,“使用未分配的局部变量”错误? - “Use of unassigned local variable” error even though I assign the variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM