第二章 简介VB.NET

VB.NET的程序语言语法
     正确的语法

  1. <%
        程序代码
     %>
  2. <%@ Page Language="VB" %>
    <script runat="server">
    sub Page_Load(obj as Object,e as EventArgs)
        程序代码
    end sub
    </script>
  3. <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
变量的数据类型

表达式的运算符号

程序举例:

  1. if TypeOf aThing is System.Text then
  2.     Response.write("aThing System.Text 物件)
  3. else
  4.     Response.write("aThing 不是 System.Text 物件")
  5. end if
  6. if ObjectA is ObjectB then
  7.     Response.write("ObjectAObjectB是相同的物件")
  8. else
  9.     Response.write("ObjectAObjectB是不同的物件")
  10. end if
  11. aString = "SayHi"
  12. if aString Like "S*H*" then
  13.     Response.write("YES")
  14. else
  15.     Response.write("NO")
  16. 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
子程序例:

  1. <script language="vb" runat="server">
  2. sub ComputeAverageobj as Objecte as EventArgs
  3.     dim Sum,Average as decimal
  4.     Sum = CDectbChinese.Text+ CDectbEnglish.Text+ CDectbMathematics.Text
  5.     Average = Sum / 3
  6.     lblMessage.Text = "平均成绩为" & Average
  7. end sub
  8. </script>
  9. <html>
  10.     <body>
  11.     <form runat="server">
  12.     请输入下列成绩<br>
  13.     语文:<asp:TextBox id="tbChinese" runat="server" /><p>
  14.     英语:<asp:TextBox id="tbEnglist" runat="server" /><p>
  15.     数学:<asp:TextBox id="tbMathematics" runat="server" /><p>
  16.     <asp:Button id="Button1" Onclick="ComputeAverage" runat="server" Text="发送" /><p>
  17.     <asp:Label id="lblMessage" runat="server" />
  18.     </from>
  19.     </body>
  20. </html>

函数的语法
    function 函数名称(参数1参数2,......) as 数据类型
             程序代码
    end function
函数举例:

  1. <script language="vb" runat="server">
  2. function IsBetween0And100num as integeras boolean
  3.     if num>=0 and num<=100 then
  4.         IsBetween0And100 = true
  5.     else
  6.         IsBetween0And100 = false
  7.     end if
  8. end funtion
  9.  
  10. sub ComputeAverageobj as Objecte as EventArgs
  11.     dim Sum,Average as Decimal
  12.     if IsBetween0And100tbChinese.Text then
  13.         if IsBetween0And100tbEnglish.Text then
  14.             if IsBetween0And100tbMathematics.Text then
  15.                 Sum = CDectbChinese.Text+ CDectbEnglish.Text+CDectbMathematics.Text
  16.                 Average = Sum / 3
  17.                 lblMessage.Text = "平均成绩为" & Average
  18.             else
  19.                                 Response.Write("数学成绩超出范围")
  20.             end if
  21.         else
  22.             Response.Write("英文成绩超出范围")
  23.         end if
  24.     else
  25.         Response.Write("语文成绩超出范围")
  26.     end if
  27. end sub
  28. </script>   

内部函数
    Math物件中的数学函数
    Strings物件中的字符串函数
    一般字符串处理函数
    日期函数
    数据类型转换函数
Math物件中的数学函数

Strings物件中的字符串函数

一般字符串函数

日期函数

类型转换函数

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页