Download presentation
Presentation is loading. Please wait.
Published byRoy Wilson Modified over 9 years ago
1
Chapter 3 Visual Basic.Net Visual Basic, like most programming languages, uses variables for storing values. Variables have a name (the word you use to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store). Arrays can be used to store indexed collections of related variables. Introduction to Variables, Constants and Data Types
2
Chapter 3 Visual Basic.Net Constants also store values, but as the name implies, those values remain constant throughout the execution of an application. Data types control the internal storage of data in Visual Basic. Introduction to Variables, Constants and Data Types
3
Chapter 3 Visual Basic.Net Variables ( 變數 ) To declare a variable is to tell the program about it in advance. You declare a variable with the Dim statement, supplying a name for the variable: 宣告變數 Dim variablename [As type] Dim shtAge As Short Dim shtAge, shtHeight, shtWeight As Short Dim shtAge As Short, strAddress As String 省略則為 Object 型態
4
Chapter 3 Visual Basic.Net 一個變數名稱不能超過 255 個字元 第一個字元必須是字母 (A-Z) ,大小寫均可或中文名稱 ,除第一個字元外,其它字元可以為 A-Z, a-z, 0-9, 或 _ 等。 Variable Name 不可以是 VB.Net 的保留字 變數名稱最後一個字元可用型態宣告字元,如 %, $, &,!,# Variable Name
5
Chapter 3 Visual Basic.Net Variable Type See Page 83 資料型態儲存大小 (Byte) 說明 Char2 字元 String10+ 字串 Short2 精簡整數 Integer4 (2) 整數 Long8 (4) 長整數 Single4 單精準度浮點數 Double8 雙精準度浮點數 Boolean4 布林 Object4 物件 Decimal12 數值 Date8 日期 Byte1 位元
6
Chapter 3 Visual Basic.Net Variable 的初值設定 Dim strUserName As String = “Charles” Dim shtAge As Short=30 Question: Dim shtAge As Short=30, shtWeight As Short
7
Chapter 3 Visual Basic.Net 型態宣告字元 Dim intIncome% v.s. Dim intIncome As Integer 資料型態 態宣告字元 Single Double Integer Long String !#%&$!#%&$
8
Chapter 3 Visual Basic.Net 型態宣告字元 Question: Dim intIncome%, shtAge As Single Dim intIncome%, shtAge!
9
Chapter 3 Visual Basic.Net <% dim shtHeight as Short dim shtWeight as Short shtHeight=173 shtWeight=(shtHeight-80)*0.7 response.write(shtWeight) %>
10
Chapter 3 Visual Basic.Net VB 運算子 + 7 - 7 * 4 / 4 - ( 負數 ) 3 \ ( 整數除法 ) 5 Mod ( 餘數 ) 6 ^ 2 & ( 字串連接 ) 8 ( ) 1 Ex : 100^2-50*3/5+(10/2)^5\9 優先順序
11
Chapter 3 Visual Basic.Net VB.NET 新增運算子 += A+=1 A=A+1 -= B-=1 B=B-1 *= C*=2 C=C*2 /= D/=2 D=D/2 \= E\=3 E=E\3 ^= F^=2 F=F^2 &= G&=H G=G&H
12
Chapter 3 Visual Basic.Net Constant The syntax for declaring a constant is: Const constantname [ As type ] = expression Const conPi = 3.14159265358979 Const conMaxPlanets As Integer = 9
13
Chapter 3 Visual Basic.Net <% Dim shtR As Short=10 Response.Write("Round:") Response.Write(2*3.14159*shtR) Response.Write(" Area:") Response.Write(2*3.14159*(shtR^2)) %>
14
Chapter 3 Visual Basic.Net <% Const cnPI=3.14159 Dim shtR As Short=10 Response.Write("Round:") Response.Write(2*cnPI*shtR) Response.Write(" Area:") Response.Write(2*cnPI*(shtR^2)) %>
15
Chapter 3 Visual Basic.Net Arrays ( 陣列 ) Arrays allow you to refer to a series of variables by the same name and data type and to use a number (an index, 索引 ) to tell them apart. Arrays allow you to refer to a series of variables by the same name and data type and to use a number (an index, 索引 ) to tell them apart. This helps you create smaller and simpler code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number. This helps you create smaller and simpler code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number.
16
Chapter 3 Visual Basic.Net In Visual Basic there are two types of arrays: –a fixed-size array which always remains the same size –a dynamic array whose size can change at run-time. Arrays ( 陣列 ) Redim 指令
17
Chapter 3 Visual Basic.Net Arrays ( 陣列 ) When declaring an array, follow the array name by the upper bound in parentheses. The upper bound cannot exceed the range of a Long data type (2^64 - 1). Dim 陣列名稱(元素數量) [As Type] Dim 陣列名稱() [As Type] = {V1, V2, … } Ex: Dim Counters(14) As Integer ' 15 elements. Dim Sums(20) As Double ‘ 21 elements. Dim shtAge() As Short = {20,21,22,23,24}
18
Chapter 3 Visual Basic.Net Multidimensional Arrays ( 多維陣列 ) With Visual Basic, you can declare arrays of multiple dimensions. With Visual Basic, you can declare arrays of multiple dimensions. Example: Dim score(2, 30) As integer
19
Chapter 3 Visual Basic.Net 1. The ReDim statement can appear only in a procedure. Unlike the Dim and Static statements, ReDim is an executable statement 2. The ReDim statement supports the same syntax used for fixed arrays. Each ReDim can change the number of elements, as well as the lower and upper bounds, for each dimension. However, the number of dimensions in the array cannot change. Example: ReDim DynArray(12) 3. The bounds of a dynamic array can be set using variables: Example: ReDim Matrix1(X) Allocate the actual number of elements with a ReDim statement Allocate the actual number of elements with a ReDim statement
20
Chapter 3 Visual Basic.Net Preserving the Contents of Dynamic Arrays Preserving the Contents of Dynamic Arrays Each time you execute the ReDim statement, all the values currently stored in the array are lost. Visual Basic resets the values to the Empty value (for Variant arrays), to zero (for numeric arrays), to a zero-length string (for string arrays), or to Nothing (for arrays of objects). This is useful when you want to prepare the array for new data, or when you want to shrink the size of the array to take up minimal memory. Sometimes you may want to change the size of the array without losing the data in the array. Syntax: ReDim Preserve ArrayName(Index)
21
Chapter 3 Visual Basic.Net 物件型態陣列 Dim objStudent(4) As Object objStudent(0)= “Lin” objStudent(1)=“Hello” objStudent(2)=29 objStudent(3)=#10/03/1973# Example:
22
Chapter 3 Visual Basic.Net VB.NET (ASP.NET) 之資料輸入與輸出 網頁資料的輸出 Response.Write(“string”) 網頁資料的輸入 變數 = Request(“ 參數名稱 ”)
23
Chapter 3 Visual Basic.Net <% dim strName As String strNAme=request("MyNAme") response.write("Hello, ") response.write(strName) %> http://127.0.0.1/CH03/EX03.aspx?MyName=John
24
Chapter 3 Visual Basic.Net 資料型別的轉換 VB.NET 強型別語言 二個不同型態資料要作處理需轉換成相同的資料型態 P.100 型別轉換函式 To 方法 P.102
25
Chapter 3 Visual Basic.Net 程式的續行與註解 Response.Write(CStr(sngFeet) & “ 英呎 ” & _ CStr(sngInches) & “ 英吋等於 ” & _ CStr(sngCentimeters) & “ 公分 ”) 續行符號 ‘ ( 上單引號 ) REM 註解 必需再新的一行
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.