字段永远不会分配给并且将始终具有其默认值 0 [英] Field is never assigned to and will always have its default value 0

查看:21
本文介绍了字段永远不会分配给并且将始终具有其默认值 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码出现以下错误,但不知道为什么:

I get the following error in my code and I'm not sure why:

警告 - 'SummaryForm.m_difficulty' 永远不会分配给,并且将始终具有其默认值 0

代码

public partial class SummaryForm : Form
{
    // Declares variables with the values pulled from the 'MainForm'
    int iCorrectACount = MainForm.iCorrectACount;
    int iCurrentQIndex = MainForm.iCurrentQIndex;

    private Difficulty m_difficulty;

    public SummaryForm()
    {

        InitializeComponent();

        double modifier = 1.0;
        if (m_difficulty == Difficulty.Easy) { modifier = 1.0; }
        if (m_difficulty == Difficulty.Medium) { modifier = 1.5; }
        if (m_difficulty == Difficulty.Hard) { modifier = 2; }

        // Sets the labels using integer values
        lblCorrectNum.Text = iCorrectACount.ToString();
        lblWrongNum.Text = (iCurrentQIndex - iCorrectACount).ToString();
        lblScoreTotal.Text = (iCorrectACount * modifier).ToString();
    }

也许这与为什么 lblScoreTotal.Text 不会更改为 value * 修饰符但会更改为另一种形式有关?

Maybe this has something to do with why lblScoreTotal.Text will not change to the value * modifier but will on another form?

我在这里问这个问题的原因是因为有人建议我禁用警告消息,但我认为这不是合适的解决方案?

The reason I asked this question here is because someone advised me to disable warning messages but I didn't think that was the appropriate solution?

谢谢.

推荐答案

m_difficulty 是私有的,所以不能从你的类外部访问它,但你永远不会在内部分配它,所以它会永不改变.

m_difficulty is private, so it can't be accessed from outside your class, but you never assign it inside, so it will never change.

因此,比较它没有任何意义,因为它总是等于 0.

Therefore, it makes no real sense to compare it, as it will always be equal to 0.

这篇关于字段永远不会分配给并且将始终具有其默认值 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆