不再使用 set 和 let 關鍵字,除非在 class 檔案中設定屬性。因為在 visual basic.net 中不再使用預設(default)屬性和方法,所以不需要靠他們來辨別,你在使用物件的方法與屬性時,若要設定變數值,一定要指明清楚。也就是原先在 vb6 若要設定變數 obj 內容為 objthis,必須要
在 vb 6 要撰寫一個物件的屬性會利用以下的程式碼,其中 property get 是用來讓使用者讀取屬性,而 property set/let 則是用來設定屬性,至於是要使用 set 還是 let 則要看賦予的屬性是物件還是一般的變數型別。
private m_myproperty as variant
public property get myproperty() as variant if isobject(m_myproperty) then set myproperty = m_myproperty else myproperty = m_myproperty end if
end property
public property set myproperty(byval vnewvalue as variant) set m_myproperty = vnewvalue
end property
public property let myproperty(byval vnewvalue as variant) m_myproperty = vnewvalue
end property
但在 visual basic.net 的程式碼要改成
private m_myproperty as string
public property myproperty() as string get myproperty = m_myproperty end get
public readonly property myproperty() as string get myproperty = m_myproperty end get
end property
或是
public writeonly property myproperty() as string set m_myproperty = value end se
t end property
subroutines 和 functions
visual basic.net 在呼叫與使用 subroutine 和 function 上也有許多改變
■ 方法呼叫的語法改變 —
所有的方法、subroutine 和 function 的呼叫都需要加上小括號,在以往若呼叫 function 但不需要 function 的回傳值﹔或 function 不需要傳入參數,可以使用不加小括號的方式,或呼叫 subroutine 不使用小括號等等是可以的,但在 visual basic.net 都不再允許。
dim rec as recordset
set rec = new recordset
rec.open "select region from northwind.dbo.customers where customerid=alfki", "provider=sqloledb;data souce=(local);initail catalog=northiwnd;user id=sa;"
dim res as variant
res = rec.fields(0)
if isnull(res) then msgbox "內容是 null"
else msgbox res
end if