获取和设置不起作用
我正在编写以下 get 和 set 来验证文本框的输入。基本上它应该检查用户是否输入了所有值。 当我将文本框留空时,它不会执行任何操作,并在使用该变量的输出中显示“0”。然而,它确实显示系统生成的异常并停止执行,但我想知道为什么它不通过属性验证输入?
这是我的代码:
public double RecoDoseSize
{
get
{
return recoDoseSize;
}
set
{
if (!(value>0))
{
MessageBox.Show("Please Enter the recommended dose size for this product");
textBox8.Focus();
}
recoDoseSize = value;
}
}
private void Submit2_Click(object sender, RoutedEventArgs e)
{
TotalContentProduct = double.Parse(textBox7.Text);
recoDoseSize = double.Parse(textBox8.Text);
NoOfDosespUnit = TotalContentProduct/recoDoseSize;
}
I am writing the following get and set for validating an input from a Text Box. Basically it is supposed to check if the user has entered all of the values.
When I leave the TextBoxes empty , it does nothing and shows a '0' in output where that variable was being used. It does however show the system generated exception and stops the execution, but I wonder why doesn't it validate the input through the properties?
Here is my code:
public double RecoDoseSize
{
get
{
return recoDoseSize;
}
set
{
if (!(value>0))
{
MessageBox.Show("Please Enter the recommended dose size for this product");
textBox8.Focus();
}
recoDoseSize = value;
}
}
private void Submit2_Click(object sender, RoutedEventArgs e)
{
TotalContentProduct = double.Parse(textBox7.Text);
recoDoseSize = double.Parse(textBox8.Text);
NoOfDosespUnit = TotalContentProduct/recoDoseSize;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在设置
recoDoseSize
(支持字段),而不是RecoDoseSize
(其中包含您的代码的属性)。因此,您的代码不会被执行。您需要将方法主体的第二行更改为(注意大写的
R
)。You are setting
recoDoseSize
, the backing field, notRecoDoseSize
, the property which has your code in it. Thus, your code isn't executed. You need to change the second line of your method body to(note the capital
R
).其他人已经给出了上述问题的正确答案。也就是说,如果您想使用 getter/setter,则应该调用大写的
RecoDoseSize
。然而,在设置器内显示消息框是非常糟糕的做法,因为它违反了最小意外原则。
当有人查看
RecoDoseSize = double.Parse(textBox8.Text);
行时,这一操作可能会导致出现消息框,这一点并不明显。偶尔会有例外,让 setter 触发 UI 更改确实有意义(例如控件上的
Visible
属性),但默认情况下应始终不执行此操作,除非您确定它会更有效。不这样做会让人感到困惑(例如,如果您设置Visible = false
但它仍然可见,那会令人惊讶)。关于您对如何实现它的评论,检查应该在点击处理程序中完成,并且该属性可以只是一个自动属性,如下所示:
您将需要使用 TryParse 因为使用 Parse 如果出现以下情况,您将收到错误该文本不是有效的
double
。 TryParse 的作用是根据是否成功返回true
或false
,如果成功,它会使用结果填充 out 参数。因此,如果它无法解析结果,或者结果是
<= 0
,它会显示消息框。在这种情况下,它也会从该方法返回,因此其余部分不会被执行。或者,该方法的其余部分可以位于else
块中,在这种情况下不需要return
。这只是一种风格的问题,更喜欢哪种方式。Other have given the correct answer to the question as stated. Namely that you should call the uppercased
RecoDoseSize
if you want to use the getter/setter.However it is extremely bad practice to show a message box inside the setter, because it violates the Principle of Least Surprise.
When someone looks at the line
RecoDoseSize = double.Parse(textBox8.Text);
it is not at all obvious that this operation could cause a message box to appear.There are occasionally exceptions where it does make sense to have a setter trigger UI changes (for instance the
Visible
property on controls) however the default should always be to not do this unless you are sure it will be more confusing to not do so (for instance it would be surprising if you setVisible = false
however it was still visible).Regarding your comment on how you should implement it, the checking should be done in the click handler and the property can just be an auto-property, like so:
You'll want to use TryParse because with Parse you'll get an error if the text isn't a valid
double
. What TryParse does is returntrue
orfalse
depending on whether it succeeded, and it populates the out parameter with the result if it's successful.So what this does is if it either failed to parse the result, or the result is
<= 0
it shows the message box. In that case it alsoreturn
s from the method so the rest of it isn't executed. Alternatively the rest of the method could be in anelse
block in which case thereturn
isn't needed. It's a matter a style which way is preferred.您实际上从未使用过 getter/setter。您直接使用实际字段名称:
recoDoseSize
。将其更改为RecoDoseSize
。You're never actually using the getter/setter. You are using the actual field name:
recoDoseSize
directly. Change it toRecoDoseSize
.您不应该在 set 语句中处理焦点。
另外,您需要确保该值不为空,否则您无法将其与任何内容进行比较(大于等)。
You shouldn't be handling focus in your set statement.
Also, you need to make sure that value is not null, otherwise you can't compare it to anything (greater-than, etc.).