获取和设置不起作用

发布于 2024-11-03 11:15:19 字数 724 浏览 4 评论 0原文

我正在编写以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

涫野音 2024-11-10 11:15:19

您正在设置 recoDoseSize(支持字段),而不是 RecoDoseSize(其中包含您的代码的属性)。因此,您的代码不会被执行。您需要将方法主体的第二行更改为

RecoDoseSize = double.Parse(textBox8.Text);

(注意大写的R)。

You are setting recoDoseSize, the backing field, not RecoDoseSize, 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

RecoDoseSize = double.Parse(textBox8.Text);

(note the capital R).

不弃不离 2024-11-10 11:15:19

其他人已经给出了上述问题的正确答案。也就是说,如果您想使用 getter/setter,则应该调用大写的 RecoDoseSize

然而,在设置器内显示消息框是非常糟糕的做法,因为它违反了最小意外原则。

当有人查看 RecoDoseSize = double.Parse(textBox8.Text); 行时,这一操作可能会导致出现消息框,这一点并不明显。

偶尔会有例外,让 setter 触发 UI 更改确实有意义(例如控件上的 Visible 属性),但默认情况下应始终不执行此操作,除非您确定它会更有效。不这样做会让人感到困惑(例如,如果您设置 Visible = false 但它仍然可见,那会令人惊讶)。

关于您对如何实现它的评论,检查应该在点击处理程序中完成,并且该属性可以只是一个自动属性,如下所示:

public double RecoDoseSize { get; set; }

private void Submit2_Click(object sender, RoutedEventArgs e)
{
    TotalContentProduct = double.Parse(textBox7.Text);

    double enteredSize;
    if (!double.TryParse(textBox8.Text, out enteredSize) || enteredSize <= 0)
    {
        MessageBox.Show("Please Enter the recommended dose size for this product");
        textBox8.Focus();
        return;
    }
    RecoDoseSize = enteredSize;
    NoOfDosespUnit = TotalContentProduct / recoDoseSize;
}

您将需要使用 TryParse 因为使用 Parse 如果出现以下情况,您将收到错误该文本不是有效的double。 TryParse 的作用是根据是否成功返回 truefalse,如果成功,它会使用结果填充 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 set Visible = 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:

public double RecoDoseSize { get; set; }

private void Submit2_Click(object sender, RoutedEventArgs e)
{
    TotalContentProduct = double.Parse(textBox7.Text);

    double enteredSize;
    if (!double.TryParse(textBox8.Text, out enteredSize) || enteredSize <= 0)
    {
        MessageBox.Show("Please Enter the recommended dose size for this product");
        textBox8.Focus();
        return;
    }
    RecoDoseSize = enteredSize;
    NoOfDosespUnit = TotalContentProduct / recoDoseSize;
}

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 return true or false 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 also returns from the method so the rest of it isn't executed. Alternatively the rest of the method could be in an else block in which case the return isn't needed. It's a matter a style which way is preferred.

我很OK 2024-11-10 11:15:19

您实际上从未使用过 getter/setter。您直接使用实际字段名称:recoDoseSize。将其更改为 RecoDoseSize

You're never actually using the getter/setter. You are using the actual field name: recoDoseSize directly. Change it to RecoDoseSize.

虫児飞 2024-11-10 11:15:19
private void Submit2_Click(object sender, RoutedEventArgs e)
{
    TotalContentProduct = double.Parse(textBox7.Text);
    RecoDoseSize= double.Parse(textBox8.Text);
    NoOfDosespUnit = TotalContentProduct/recoDoseSize;
}
private void Submit2_Click(object sender, RoutedEventArgs e)
{
    TotalContentProduct = double.Parse(textBox7.Text);
    RecoDoseSize= double.Parse(textBox8.Text);
    NoOfDosespUnit = TotalContentProduct/recoDoseSize;
}
玉环 2024-11-10 11:15:19

您不应该在 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.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文