最近,我一直在我的编程课程(教 VB)中介绍数据类型,我遇到了一个有趣的情况。在尝试演示随机数生成器时,我遇到了这样一个事实,即我的代码允许在算术语句中清晰地使用字符串。虽然我对它很好,但我正在绞尽脑汁思考理由或实际导致这种情况发生的原因。
以下是我为测试而构建的一些代码示例:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Produces '56'
MsgBox("5" + "6")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Produces 11
MsgBox(5 + 6)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'Produces 11
MsgBox("5" + 6)
End Sub
Private Sub RandomNumber(ByVal low As Integer, ByVal high As Integer)
Randomize()
MsgBox((high - low) * Rnd() + low)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Produces Random Number between 5 - 6
RandomNumber(5, "6")
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'Produces Random Number between 5 - 6
RandomNumber("5", "6")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'Produces Random Number between 5 - 6
RandomNumber(5, 6)
End Sub
End Class
如果它是 IDE 的东西,我在 Windows 7 上使用 Visual Studio 2010 Ultimate。