1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VB.NETで困ったこと(ひょっとしたらいいこと?)

Posted at

以前これではまった

メンバーが書いたコードで、単純化すると以下の様なコードがあってはまったことがあった。

ng_return_value.vb
Function FuncA() as Boolean
  Return -1
End Function

If FuncA() = True Then
  'こちらが実行
Else
  'こっちにはこない
End If

コードトレースしても一度目では気づかず、よくよく見なおして判明。
まさか、-1がTrueになるなんてそんなぁ!という私はC/C++出身。

以下の書き方には注意

ng_return_value.vb
Module Module1
    Sub Main()
        If (funcBool("10") = True) Then
            ' True
        End If

    End Sub
    Function funcBool(ByVal bVal As Boolean) As Boolean
        Return 0
        Return 1
        Return -1
        Return True
    End Function
End Module

これ、funcBool()は、戻り値Booleanで宣言しているのに、
Retrun 0 でも
Return 1 でも
Return -1 でも
エラーにならず、コンパイル(ビルド)が通ります。
もちろん、
Return True
はコンパイル通ります。

また、Boolean値をパラメータに受け取るのですが、
funcBool("10") と文字列でもコンパイルは通ります。

1
0
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?