【问题标题】:I get an error: use of unassigned local variable 'artist', don't know what i'm doing wrong?我收到一个错误:使用未分配的局部变量“艺术家”,不知道我做错了什么?
【发布时间】:2014-02-20 01:21:34
【问题描述】:
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);
        }
    }

【问题讨论】:

    标签: c#


    【解决方案1】:

    您正在使用可能未分配的变量artist。见:

    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 语句的计算结果为false,并且Artists 集合中没有对象,您将使用未分配的变量点击new Song 行。尝试将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
            }
        }
    

    【讨论】:

    • Np,如果它解决了您的问题,请不要忘记接受并投票!
    猜你喜欢
    • 2020-07-29
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多