﻿ 认识Visual Basic 的常数和变数

# 第六课： 认识VB的常数和变数

### 6.1 常数

Const  constantname As datatype=initializer

constantname 是我们自己制定的名称，datatype 是资料类型而 initializer 则是初始设定值。

Const  Num  As Integer = 9.8

Const Pi As Single  = 3.142

Const SchoolName As String  = "中华中学“

### 6.2 变数

Dim VariableName as Datatype[=initializer]

Dim Num As Single=1.234

Dim Num1,Num2  As Integer

Dim yourName As String

Dim starName As String="刘翔”

Variable=Expression

firstNumber=100
secondNumber=firstNumber-99
Label1.Visible = True
Command1.Visible = false
Label4.Caption = textbox1.Text
ThirdNumber = Val(usernum1.Text)
total = firstNumber + secondNumber+ThirdNumber

### 6.3  Visual Basic的运算符

^

2^4=16

*

4*3=12

/

12/4=3

Mod

15 Mod 4=3

\

19\4=4

+ or &

"Visual"&"Basic"="Visual Basic"

Dim firstName As String

Dim secondName As String

Dim yourName As String

Private Sub Command1_Click()

firstName = Text1.Text

secondName = Text2.Text

yourName = secondName + "  " + firstName

Label1.Caption = yourName

End Sub

Dim number1, number2, number3 as Integer

Dim total, average as variant

Private sub Form_Click

number1=val(Text1.Text)
number2=val(Text2.Text)
number3= val(Text3.Text)

Total=number1+number2+number3

Average=Total/5

Label1.Caption=Total

Label2.Caption=Average

End Sub