Chapter 3 Visual Basic Net Introduction to Variables

Chapter 3 Visual Basic. Net Introduction to Variables, Constants and Data Types 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.

Chapter 3 Visual Basic. Net Introduction to Variables, Constants and Data Types 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.

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: 省略則為 Object 型態 Dim variablename [As type] Dim sht. Age As Short Dim sht. Age, sht. Height, sht. Weight As Short Dim sht. Age As Short, str. Address As String


Chapter 3 Visual Basic. Net Variable Type See Page 83 儲存大小 (Byte) 資料型態 說明 Char 2 字元 String 10+ 字串 Short 2 精簡整數 Integer 4 (2) 整數 Long 8 (4) 長整數 Single 4 單精準度浮點數 Double 8 雙精準度浮點數 Boolean 4 布林 Object 4 物件 Decimal 12 數值 Date 8 日期 Byte 1 位元

Chapter 3 Visual Basic. Net Variable 的初值設定 Dim str. User. Name As String = “Charles” Dim sht. Age As Short=30 Question: Dim sht. Age As Short=30, sht. Weight As Short

Chapter 3 Visual Basic. Net 型態宣告字元 Dim int. Income% v. s. Dim int. Income As Integer 資料型態 Single Double Integer Long String 態宣告字元 ! # % & $

Chapter 3 Visual Basic. Net 型態宣告字元 Question: Dim int. Income%, sht. Age As Single Dim int. Income%, sht. Age!

Chapter 3 Visual Basic. Net <html> <% dim sht. Height as Short dim sht. Weight as Short sht. Height=173 sht. Weight=(sht. Height-80)*0. 7 response. write(sht. Weight) %> </html>


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=E3 F^=2 F=F^2 G&=H G=G&H

Chapter 3 Visual Basic. Net Constant The syntax for declaring a constant is: Const constantname [As type] = expression Const con. Pi = 3. 14159265358979 Const con. Max. Planets As Integer = 9

Chapter 3 Visual Basic. Net <html> <% Dim sht. R As Short=10 Response. Write("Round: ") Response. Write(2*3. 14159*sht. R) Response. Write(" Area: ") Response. Write(2*3. 14159*(sht. R^2)) %> </html>

Chapter 3 Visual Basic. Net <html> <% Const cn. PI=3. 14159 Dim sht. R As Short=10 Response. Write("Round: ") Response. Write(2*cn. PI*sht. R) Response. Write(" Area: ") Response. Write(2*cn. PI*(sht. R^2)) %> </html>

Chapter 3 Visual Basic. Net Arrays (陣列) n. 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. n. 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.

Chapter 3 Visual Basic. Net Arrays (陣列) n 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. Redim 指令

Chapter 3 Visual Basic. Net Arrays (陣列) n 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] = {V 1, V 2, … } Ex: Dim Counters(14) As Integer ' 15 elements. Dim Sums(20) As Double ‘ 21 elements. Dim sht. Age() As Short = {20, 21, 22, 23, 24}

Chapter 3 Visual Basic. Net Multidimensional Arrays (多維陣列) n. With Visual Basic, you can declare arrays of multiple dimensions. Example: Dim score(2, 30) As integer

Chapter 3 Visual Basic. Net n 1. 2. 3. Allocate the actual number of elements with a Re. Dim statement The Re. Dim statement can appear only in a procedure. Unlike the Dim and Static statements, Re. Dim is an executable statement The Re. Dim statement supports the same syntax used for fixed arrays. Each Re. Dim 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: Re. Dim Dyn. Array(12) The bounds of a dynamic array can be set using variables: Example: Re. Dim Matrix 1(X)

Chapter 3 Visual Basic. Net n Preserving the Contents of Dynamic Arrays Each time you execute the Re. Dim 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: Re. Dim Preserve Array. Name(Index)

Chapter 3 Visual Basic. Net 物件型態陣列 Example: Dim obj. Student(4) As Object obj. Student(0)= “Lin” obj. Student(1)=“Hello” obj. Student(2)=29 obj. Student(3)=#10/03/1973#

Chapter 3 Visual Basic. Net VB. NET (ASP. NET) 之資料輸入與輸出 網頁資料的輸出 Response. Write(“string”) 網頁資料的輸入 變數 = Request(“參數名稱”)

Chapter 3 Visual Basic. Net <html> <% dim str. Name As String str. NAme=request("My. NAme") response. write("Hello, ") response. write(str. Name) %> </html> http: //127. 0. 0. 1/CH 03/EX 03. aspx? My. Name=John


Chapter 3 Visual Basic. Net 程式的續行與註解 Response. Write(CStr(sng. Feet) & “ 英呎” & _ CStr(sng. Inches) & “ 英吋等於” & _ CStr(sng. Centimeters) & “公分”) 續行符號 註解 ‘ (上單引號) REM 必需再新的一行
- Slides: 25