简体   繁体   English

是/否消息框始终返回是-VB.Net

[英]Yes/No message box always returns yes - VB.Net

I was experimenting with message boxes, and tried a simple yes/no messagebox So I wrote this simple piece of code. 我正在尝试使用消息框,并尝试了一个简单的yes / no messagebox,所以我编写了这段简单的代码。 However, the "chc" variable always returns as 1, no matter what button I press. 但是,无论我按什么按钮,“ chc”变量始终返回1。 I provided the code, so you maybe see what I did wrong. 我提供了代码,所以您可能会看到我做错了。 It is probably horribly wrong. 这可能是非常错误的。

If MsgBoxResult.Yes Then
    chc = 1
ElseIf MsgBoxResult.No Then
    chc = 0
End If

MsgBox(chc)

The MsgBox() method is returning MsgboxResult Enumeration , check the value that the method returns: MsgBox()方法返回MsgboxResult Enumeration ,检查该方法返回的值:

Public Sub MsgBoxExample()
    Dim result As MsgBoxResult = Nothing
    Dim chc As Integer

    result = MsgBox("click something...", vbYesNo, "example")
    If result = MsgBoxResult.Yes Then
        chc = 1
    ElseIf result = MsgBoxResult.No Then
        chc = 0
    End If
    MsgBox(chc)
End Sub

Turn on strict compilation. 打开严格的编译。

  • You can do this for a file by placing Option Strict On at the top of your file. 您可以通过在文件顶部放置Option Strict On来对文件执行此操作。
  • You can do it for a project in project properties > Compile > Compile Options, setting Option strict to On. 您可以在项目属性>编译>编译选项中为项目执行此操作,将Option strict设置为On。
  • Make it the default for all of your projects in Tools > Options > Project and Solutions > VB Defaults. 在工具>选项>项目和解决方案> VB默认值中将其设置为所有项目的默认值。

Really, do it. 真的,做吧。 It is one if the easiest, quickest ways to catch all sorts of errors before they happen. 这是最简单,最快的方法,可以在各种错误发生之前将其捕获。 You might get hours of your life back. 您可能会恢复数小时的生活。 I'm not exaggerating. 我一点都不夸张。

With strict compilation turned off, the compiler sees this: 关闭严格编译后,编译器将看到以下内容:

If MsgBoxResult.Yes Then

And says, 'If/then checks for whether a condition is true or false. 并说:“是否/然后检查条件是对还是错。 MsgBoxResult.Yes isn't a boolean - it's an integer. MsgBoxResult.Yes不是布尔值-是整数。 But I'll convert it, and say that anything other than zero is true.' 但是我将其转换,并说除零以外的其他都是正确的。

The problem is that all values for MsgBoxResult are non-zero. 问题是MsgBoxResult所有值MsgBoxResult为零。 So even MsgBoxResult.No is "true." 因此,即使MsgBoxResult.No为“ true”。

With Option Strict On , you'll get this compiler error: 使用Option Strict On ,您将得到以下编译器错误:

Option Strict On disallows implicit conversions from 'MsgBoxResult' to 'Boolean'. Option Strict On不允许从“ MsgBoxResult”到“ Boolean”的隐式转换。

And as soon as you see it, you'll realize that you meant 一旦看到它,您就会意识到自己的意思是

If result = MsgBoxResult.Yes Then

and you'll fix it before you even run the program. 您将在运行该程序之前对其进行修复。

Compiler errors are better than runtime errors because you don't have to debug the code to figure out what's wrong, or worse, have a user report it and then have to figure out what went wrong. 编译器错误比运行时错误要好,因为您不必调试代码来找出问题所在,或者更糟的是,让用户报告它,然后必须找出问题所在。 We all make little errors, and the compiler catches them right away. 我们都犯了很少的错误,并且编译器会立即捕获它们。

When you turn on strict compiling, you might see tons of other errors. 当您打开严格编译时,您可能会看到大量其他错误。 Each and every one of those is something that could possibly cause a runtime error. 其中的每一项都可能导致运行时错误。

Having strict compiling turned off allows us to do all sorts of evil things like 关闭严格的编译功能可以使我们做各种邪恶的事情,例如

Private Sub WhatDoesThisDo(x As String, y As Integer, z As TriState)
    If x And y And z Then
        'do something
    End If
End Sub

Then if you call 那如果你打电话

`WhatDoesThisDo("x", 0, TriState.UseDefault)`

You get a runtime error, 您会遇到运行时错误,

System.InvalidCastException: 'Conversion from string "x" to type 'Long' is not valid.' 'System.InvalidCastException:'从字符串“ x”转换为'Long'类型的转换无效。

It's like having a two-direction highway instead of letting cars drive all over the place and hoping they don't hit each other. 这就像有一条双向高速公路,而不是让汽车四处行驶,并希望它们不会相互碰撞。 It's not guaranteed to prevent accidents, but it eliminates a huge potential cause. 不能保证防止发生事故,但是它消除了巨大的潜在原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM