[英]Field is never assigned to and will always have its default value 0
I get the following error in my code and I'm not sure why: 我的代码中出现以下错误,我不确定原因:
Warning - 'SummaryForm.m_difficulty' is never assigned to, and will always have its default value 0
警告 -
'SummaryForm.m_difficulty' is never assigned to, and will always have its default value 0
Code 码
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();
}
Maybe this has something to do with why lblScoreTotal.Text
will not change to the value * modifier but will on another form? 也许这与为什么
lblScoreTotal.Text
不会更改为值*修饰符但是会在另一个表单上有什么关系?
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? 我在这里问这个问题的原因是因为有人建议我禁用警告信息,但我不认为这是合适的解决方案?
Thanks. 谢谢。
The compiler is entirely correct: nothing is going to change your m_difficulty
field, as far as you've shown. 编译器是完全正确的:就你所示, 没有任何东西可以改变你的
m_difficulty
字段。 What do you expect to set that value? 您期望设定该值是什么? Did you actually mean to set it to something based on
MainForm
as per iCorrectACount
and iCurrentQIndex
? 您是否真的想根据
iCorrectACount
和iCurrentQIndex
将其设置为基于MainForm
东西?
How do you expect it would ever be anything other than whatever (Difficulty) 0
evaluates as? 你怎么看待它除了任何其他东西
(Difficulty) 0
评价为什么?
It's pretty dodgy to be pulling initial values from a statically-accessed instance of a form, too, IMO. 从静态访问的表单实例中提取初始值也是非常狡猾的,IMO。 It would be much better if the constructor accepted initial values from whatever was constructing it.
如果构造函数接受构造它的任何东西的初始值会好得多。
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. m_difficulty
是私有的,所以它不能从你的课外访问,但你永远不会在里面分配它,所以它永远不会改变。
Therefore, it makes no real sense to compare it, as it will always be equal to 0. 因此,比较它没有任何意义,因为它总是等于0。
You should always initialize a variable after you declared it. 在声明变量后,应始终初始化变量。
private Difficulty m_difficulty = new Difficulty();
Something like that. 这样的事情。
So you prevent it to be null (in this case you would get an exception). 所以你要防止它为null(在这种情况下你会得到一个例外)。
The warning just tells you this. 警告只是告诉你这个。
It sounds to me like you're expecting m_difficulty to be bound to a dropdown selection on your user form. 听起来像你期望m_difficulty绑定到用户表单上的下拉选项。 It is not.
它不是。 Even if it were, you would want to access the SelectedValue property instead of the object itself.
即使它是,您也希望访问SelectedValue属性而不是对象本身。 Maybe this is what you're looking for.
也许这就是你要找的东西。
Difficulty m_difficulty = (Difficulty)Enum.Parse(ddDifficulty.SelectedValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.