Vb.net的資料形態: 變數 (1) 變數的宣告: 變數的宣告為暫時儲存資料的空間供程式使用 => 變數必須以字母或為開頭 => 變數僅為字母數字及底線字 => 不可visual basic 使用關鍵字 => 不可在同一範圍中有相同的變數名稱 (2) 變數的宣告: => asp.net 中變數必須宣告才可使用 (asp 可不用宣告) 語法: Dim 變數名稱[,變數名稱…..] as 資料型態〔=初值 〕 =>(asp 只有一種資料型態故以 dim變數名稱) 例: Dim i Dim I as integer Dim I,j as integer Dim I as integer=1,j as integer=12 Dim I,j as integer
Dim y As Byte = 12 Dim y1 As SByte = 110 Dim x As Integer Dim x1 As ULong Dim x2 As UShort Dim x3 As Single = 1.0E+23 Dim x4 As Double = 1 Dim x5 As Double = 1.23 Dim x6 As Decimal = 12 Dim x7 As Date = #1/2/0003# Dim x8 As String = "ccck" Dim x9 As Char = "a"c Dim x10 As Boolean = False Dim x11 As Object Dim xx As Integer = CInt(1.23) Dim xx1 As Integer = 1 Dim xx2 As String = 1 & "xx2"
一維陣列: (1) dim 陣列名稱() as 資料型態 = {初值列,…..} Dim陣列名稱(陣列索引末值) as 資料型態 dim 陣列名稱() as 資料型態 =new資料型態( ){初值列,…..} dim 陣列名稱() as 資料型態 =new資料型態(陣列索引末值){初值列,…..}
二維陣列: (1) dim 陣列名稱(,) as 資料型態 = {{初值列},{….}.} Dim陣列名稱(列索引末值, 行索引末值) as 資料型態 dim 陣列名稱() as 資料型態 =new資料型態( ){{初值列} , {…..},..} dim 陣列名稱() as 資料型態 =new資料型態(列索引末值, 行索引末值){{初值列},{….},.}
三維陣列: (1) dim 陣列名稱() as 資料型態 = {{{初值列},{….},{...}}} Dim陣列名稱(深度索引末值, 列索引末值, 行索引末值) as 資料型態 dim 陣列名稱() as 資料型態 =new資料型態( ) {{{初值列},{….},{...}}} dim 陣列名稱() as 資料型態 =new資料型態(深度索引末值, 列索引末值, 行索引末值) {{{初值列},{….},{...}}}
範例
Dim arr(2) As Integer Dim arr1() As Integer = {1, 2, 3} Dim arr2(1, 2) As Integer Dim arr3(,) As Integer = {{1}, {2}} Dim arr4(1, 2, 3) As Integer Dim arr5(,) As Integer = {{1, 2}, {3, 4}} Dim arr6(,,) As Integer = {{{1}, {2}}, {{3}, {4}}} Dim arr7() As Integer = New Integer() {1, 2, 3} Dim arr77() as integer=new integer(2){1,2,3} Dim arr8(,) As Integer = New Integer(,) {{1, 2}, {3, 4}} Dim arr9(,,) As Integer = New Integer(,,) {{{1}, {2}}, {{3}, {4}}}
集合物件: 1. 物件組合形成集合物件 2. vb中可使用 arraylist 的類別建立一集合物件 3. 建立一集合物件 Dim 物件變數 as new arraylist 4. 集合物件常用的方法及屬性 add,clear,remove,insert,count,item,sort 5. 一般可用for each迴圈處理
範例
Dim custlist As New ArrayList custlist.Add(1) custlist.Add(1.23) custlist.Add("a") Response.Write(custlist(0)) Response.Write(custlist(1)) Response.Write(custlist.Item(2).ToString)
Dim i As Integer For i = 0 To custlist.Count - 1 Response.Write(custlist(i))
Next Dim ele As Object For Each ele In custlist Response.Write(ele.ToString) Next
陣列及集合物件 1. 陣列亦為一集合物件亦可使用集合物件方式處理 2. 陣列除了使用回圈處理亦可使用for each 的結構
範例
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim arr() As Integer = {1, 2, 3} For Each ele As Integer In arr Response.Write(ele) Next Dim arr2(,) As Integer = New Integer(1, 1) {{1, 2}, {3, 4}} For Each ele As Integer In arr2 Response.Write(ele) Next
End Sub
組合資料型態(不同的資料型態):
結構 structure: a) 一般為靜態的資料組合,建立一結構變數 b) 資料成員預設為一public 存取層級 c) 資料成員宣告與一般變數宣告一樣 d) 存取結構的成員資料使用點運算子 e) 結構可形成巢狀結構
結構宣告: =>
Structure 結構名稱 資料成員1 資料成員2 Dim 變數名稱 as 資料型態 …. End structure
結構變數的宣告: => Dim 結構變數名稱 as [new] 結構資料型態
範例1: Structure man Dim age As Integer Dim weight As Integer Dim height As Integer Dim name As String End Structure
Protected Sub page_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles mybase.load Dim redman As man redman.age = 1 : redman.weight = 2 : redman.height = 3 : redman.name = "chiang" Response.Write(redman.age & redman.weight & redman.height & redman.name) End Sub
範例1: structure person Dim blueman As man End Structure …… Dim blueperson As person blueperson.blueman.show(1) Response.Write(blueperson.blueman.age) ………
類別(class): 物件的模板,亦為一資料組合型態其類別成員除為靜態的資料組合外亦包括方法 a) 成員一般為靜態的資料物件的方法,建立一物件實體才可參照成員 b) 資料成員預設為一private 存取層級 c) 靜態資料成員宣告與一般變數宣告一樣 ,方法為一sub 或是function程序 d) 存取物件的成員使用點運算子 e) 類別可形成巢狀結構或複合結構
類別宣告: =>
class 結構名稱 資料成員1 資料成員2 Dim 變數名稱 as 資料型態 Public sub 方法名稱(arglist….) … End sub …. End class
範例 Class man Public age As Integer Public weight As Integer Public height As Integer Public name As String Public Sub show(ByVal x As Integer) age = age + 212100 End Sub End Class class person Dim blueman As new man End class
物件實體的建立與成員的引用: => Dim 結構變數名稱 as new 結構資料型態 =web form. 無法使用動態新增控制項物件及web form => 因web form 是由html描述邏輯程式的物件類別由.net frame compile => 書出結果會在檔案的最前面 => dim obj as new system.web.ui.button Me.controls.add(obj) Dim obj1 as new system.web.ui.page Obj1.show( 不支援)
範例
Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim redman As New man redman.age = 1 : redman.weight = 2 : redman.height = 3 : redman.name = "chiang"
沒有留言:
張貼留言