VB.NET的程序语言语法:
正确的语法
- <%
程序代码
%> - <%@ Page Language="VB" %>
<script runat="server">
sub Page_Load(obj as Object,e as EventArgs)
程序代码
end sub
</script> - <script Language="VB" runat="server">
sub Page_Load(obj as Object,e as EventArgs)
程序代码
end sub
</script>
错误的语法
<%@ Page Language="VB" %>
<script runat="server">
程序代码
</script>
变量的声明:
语法: dim 变量名称 as 数据类型
[例]dim i,j as integer
dim strText as string
dim blnGo as boolean = true
dim params(10) as string
变量的数据类型:
表达式的运算符号
程序举例:
- if TypeOf aThing is System.Text then
- Response.write("aThing 是 System.Text 物件)
- else
- Response.write("aThing 不是 System.Text 物件")
- end if
- if ObjectA is ObjectB then
- Response.write("ObjectA和ObjectB是相同的物件")
- else
- Response.write("ObjectA和ObjectB是不同的物件")
- end if
- aString = "SayHi"
- if aString Like "S*H*" then
- Response.write("YES")
- else
- Response.write("NO")
- end if
表达式(Statements)
Statement的断行(区别&)
错误:<% 正确:<%
Response.Write Response.Write _
("Hello World") ("Hello World")
%> %>
错误:<% 正确:<%
Response.Write ("Hello _ Response.Write ("Hello" _
World") "World")
%> %>
错误:<input type="text" _ 正确:<input type="text"
size="8" _ size="8"
name="inName"> name="inName">
select Statements(switch)
select case 条件语句
case 结果1
程序代码
case 结果2
程序代码
.....
case else
程度代码
end select
Do Statements(循环)
1. do while 条件语句 2. do until 条件语句
程序代码 程序代码
loop loop
3. do 4. do
程序代码 程序代码
loop while 条件语句 loop until 条件语句
For Statements(循环)
1. for 数值变量=起始值 to 结束值 [step 间隔值] 2. for each 物体变量 in 物体或数组名称
程序代码 程序代码
next 数值变量(和上面的同一个,[]可省去) next
while Statements(循环) 子程序的语法
while 条件语句 sub 子程序名称(参数1,参数2,......)
程序代码 程序代码
end while end sub
子程序例:
- <script language="vb" runat="server">
- sub ComputeAverage(obj as Object,e as EventArgs)
- dim Sum,Average as decimal
- Sum = CDec(tbChinese.Text)+ CDec(tbEnglish.Text)+ CDec(tbMathematics.Text)
- Average = Sum / 3
- lblMessage.Text = "平均成绩为" & Average
- end sub
- </script>
- <html>
- <body>
- <form runat="server">
- 请输入下列成绩<br>
- 语文:<asp:TextBox id="tbChinese" runat="server" /><p>
- 英语:<asp:TextBox id="tbEnglist" runat="server" /><p>
- 数学:<asp:TextBox id="tbMathematics" runat="server" /><p>
- <asp:Button id="Button1" Onclick="ComputeAverage" runat="server" Text="发送" /><p>
- <asp:Label id="lblMessage" runat="server" />
- </from>
- </body>
- </html>
函数的语法:
function 函数名称(参数1,参数2,......) as 数据类型
程序代码
end function
函数举例:
- <script language="vb" runat="server">
- function IsBetween0And100(num as integer)as boolean
- if num>=0 and num<=100 then
- IsBetween0And100 = true
- else
- IsBetween0And100 = false
- end if
- end funtion
- sub ComputeAverage(obj as Object,e as EventArgs)
- dim Sum,Average as Decimal
- if IsBetween0And100(tbChinese.Text) then
- if IsBetween0And100(tbEnglish.Text) then
- if IsBetween0And100(tbMathematics.Text) then
- Sum = CDec(tbChinese.Text)+ CDec(tbEnglish.Text)+CDec(tbMathematics.Text)
- Average = Sum / 3
- lblMessage.Text = "平均成绩为" & Average
- else
- Response.Write("数学成绩超出范围")
- end if
- else
- Response.Write("英文成绩超出范围")
- end if
- else
- Response.Write("语文成绩超出范围")
- end if
- end sub
- </script>
内部函数:
Math物件中的数学函数
Strings物件中的字符串函数
一般字符串处理函数
日期函数
数据类型转换函数
Math物件中的数学函数
Strings物件中的字符串函数
一般字符串函数
日期函数
类型转换函数