chapter 3 visual basic.net visual basic, like most programming languages, uses variables for storing...

25
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

Upload: roy-wilson

Post on 04-Jan-2016

224 views

Category:

Documents


3 download

TRANSCRIPT

Page 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

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

Page 2: 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

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

Page 3: 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

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 ShortDim shtAge, shtHeight, shtWeight As ShortDim shtAge As Short, strAddress As String

省略則為 Object 型態

Page 4: 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

Chapter 3 Visual Basic.Net

•一個變數名稱不能超過 255 個字元•第一個字元必須是字母 (A-Z) ,大小寫均可或中文名稱 ,除第一個字元外,其它字元可以為 A-Z, a-z, 0-9, 或 _ 等。•Variable Name 不可以是 VB.Net 的保留字 •變數名稱最後一個字元可用型態宣告字元,如 %, $ , &,!,#

Variable NameVariable Name

Page 5: 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

Chapter 3 Visual Basic.Net

Variable TypeVariable TypeSee 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 位元

Page 6: 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

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

Page 7: 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

Chapter 3 Visual Basic.Net

型態宣告字元Dim intIncome% v.s.Dim intIncome As Integer

資料型態 態宣告字元SingleDoubleIntegerLongString

!#%&$

Page 8: 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

Chapter 3 Visual Basic.Net

型態宣告字元

Question:

Dim intIncome%, shtAge As SingleDim intIncome%, shtAge!

Page 9: 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

Chapter 3 Visual Basic.Net

<html>

<%

dim shtHeight as Short

dim shtWeight as Short

shtHeight=173

shtWeight=(shtHeight-80)*0.7

response.write(shtWeight)

%>

</html>

Page 10: 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

Chapter 3 Visual Basic.Net

VB 運算子

+ 7- 7* 4/ 4 - ( 負數 ) 3\ ( 整數除法 ) 5Mod ( 餘數 ) 6^ 2& ( 字串連接 ) 8( ) 1

Ex : 100^2-50*3/5+(10/2)^5\9

優先順序

Page 11: 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

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

Page 12: 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

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

Page 13: 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

Chapter 3 Visual Basic.Net

<html>

<%

Dim shtR As Short=10

Response.Write("Round:")

Response.Write(2*3.14159*shtR)

Response.Write("<br>Area:")

Response.Write(2*3.14159*(shtR^2))

%>

</html>

Page 14: 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

Chapter 3 Visual Basic.Net

<html>

<%

Const cnPI=3.14159

Dim shtR As Short=10

Response.Write("Round:")

Response.Write(2*cnPI*shtR)

Response.Write("<br>Area:")

Response.Write(2*cnPI*(shtR^2))

%>

</html>

Page 15: 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

Chapter 3 Visual Basic.Net

Arrays ( 陣列 )

Arrays allow you to refer to a series of variables by tArrays allow you to refer to a series of variables by the same name and data type and to use a number (ahe same name and data type and to use a number (an index, n index, 索引索引 ) to tell them apart.) to tell them apart.This helps you create smaller and simpler code in mThis helps you create smaller and simpler code in many situations, because you can set up loops that deaany situations, because you can set up loops that deal efficiently with any number of cases by using the indl efficiently with any number of cases by using the index number. ex number.

Page 16: 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

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 指令

Page 17: 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

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}

Page 18: 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

Chapter 3 Visual Basic.Net

Multidimensional Arrays (多維陣列 )

With Visual Basic, you can declare With Visual Basic, you can declare arrays of multiple dimensions. arrays of multiple dimensions.

Example: Dim score(2, 30) As integer

Page 19: 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

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 Allocate the actual number of elements with a ReDim statementwith a ReDim statement

Page 20: 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

Chapter 3 Visual Basic.Net

Preserving the Contents of Dynamic ArraysPreserving 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)

Page 21: 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

Chapter 3 Visual Basic.Net

物件型態陣列

Dim objStudent(4) As ObjectobjStudent(0)= “Lin”objStudent(1)=“Hello”objStudent(2)=29objStudent(3)=#10/03/1973#

Example:

Page 22: 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

Chapter 3 Visual Basic.Net

VB.NET (ASP.NET) 之資料輸入與輸出

網頁資料的輸出

Response.Write(“string”)

網頁資料的輸入

變數 = Request(“ 參數名稱” )

Page 23: 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

Chapter 3 Visual Basic.Net

<html>

<%

dim strName As String

strNAme=request("MyNAme")

response.write("Hello, ")

response.write(strName)

%>

</html>

http://127.0.0.1/CH03/EX03.aspx?MyName=John

Page 24: 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

Chapter 3 Visual Basic.Net

資料型別的轉換

VB.NET 強型別語言

二個不同型態資料要作處理需轉換成相同的資料型態

P.100

型別轉換函式

To 方法

P.102

Page 25: 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

Chapter 3 Visual Basic.Net

程式的續行與註解

Response.Write(CStr(sngFeet) & “ 英呎” & _CStr(sngInches) & “ 英吋等於” & _CStr(sngCentimeters) & “ 公分” )

續行符號

‘ ( 上單引號 )REM註解

必需再新的一行