vb.net小问题
如代码,这是个CLICK事件:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r, s As String
r = TextBox1.Text
s = r ^ 2 * 3.1415
TextBox2.Text = s
问题是为什么不能改成这样:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.text=TextBox1.texe^2*3.1415 '(省去了定义变量)
我测试过,这样肯定不成立,是不是很个对象或控件都必须要先声明某个类型?
我是菜鸟,恳求高手指点迷津!!
参考答案:把Dim r, s As String
r = TextBox1.Text
s = r ^ 2 * 3.1415
TextBox2.Text = s
改为:
Dim r,s As Double
r = CDbl(TextBox1.Text)
s = r ^ 2 * 3.1415
TextBox2.Text = s
就好了
也就是说用一个强制类型转换就解决了!
字符和数就运算不了的.