为什么 Visual Basic 允许在算术语句中将字符串添加到整数? [英] Why does Visual Basic allow Strings to be added to Integers in arithmetic statements?

查看:33
本文介绍了为什么 Visual Basic 允许在算术语句中将字符串添加到整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在我的编程课程(教授 VB)中介绍数据类型,但我遇到了一个有趣的情况.在尝试演示随机数生成器时,我遇到了这样一个事实:我的代码允许在算术语句中清晰地使用字符串.虽然我很乐意这样做,但我正在绞尽脑汁思考其理由或实际导致这种情况发生的原因.

Recently, I've been covering data types in my programming course (teaching VB) and I've run into an interesting situation. While attempting to demonstrate a random number generator, I ran into the fact that my code was allowing for Strings to be used legibly in arithmetic statements. While I'm fine with it doing it, I'm wracking my brain as to the justification or the what is actually causing this to happen.

下面是我为测试这个而构建的一些代码的一些示例:

Below are some examples of some code I built to test this:

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.

In case it is an IDE thing, I'm using Visual Studio 2010 Ultimate on Windows 7.

推荐答案

我猜你已经将 Option Strict 设置为 Off.

I'm guessing you have Option Strict set to Off.

查看 MSDN 中的文档 (http://msdn.microsoft.com/en-us/library/9c5t70w2.aspx) 用于 + 运算符:

Check out the documentation from MSDN (http://msdn.microsoft.com/en-us/library/9c5t70w2.aspx) for the + operator:

"一个表达式是数字数据类型,另一个是字符串

"One expression is a numeric data type and the other is a string

如果 Option Strict 为 On,则生成编译器错误.如果 Option Strict 为 Off,则将 String 隐式转换为 Double 并添加.如果 String 无法转换为 Double,则抛出 InvalidCastException 异常."

If Option Strict is On, then generate a compiler error. If Option Strict is Off, then implicitly convert the String to Double and add. If the String cannot be converted to Double, then throw an InvalidCastException exception."

这篇关于为什么 Visual Basic 允许在算术语句中将字符串添加到整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆