VB6中有幾種長得很像的語句:Let、Set、LSet、RSet。
Let用於一般變數的賦值:
複製代碼代碼如下:
[Let] varname = expression
大部分情況下我們都省略Let,直接用等號賦值,以致於不少人根本不知道Let的存在。
Set用於物件的賦值,將變數指向物件並增加物件的參考計數,也有不少人不知道引用計數為何。
那麼LSet是做什麼用的呢?咋一看好像是Let和Set的結合體,其實不然。 LSet中的L是Left的縮寫,與之對應的是RLet。你問我怎麼知道L是Left的縮寫?文件上面寫的嗆:
複製代碼代碼如下:
LSet Statement
Left aligns a string within a string variable, or copies a variable of one user-defined type to another variable of a different user-defined type.
RSet Statement
Right aligns a string within a string variable.
LSet比RSet多出一個功能,先不看這個,先看相同的部分,兩者分別用來在一字串變數中將一字串往左對齊(右對齊)。什麼意思呢?其實光看文件我也沒不懂,實際測試一下好了:
複製代碼代碼如下:
Sub Main()
Dim url As String
Dim s As String
Let url = "//www.VeVB.COm"
s = String$(20, "*")
LSet s = url
Debug.Print s
RSet s = url
Debug.Print s
End Sub
輸出(注意空格):
複製代碼代碼如下:
//www.VeVB.COm
//www.VeVB.COm
的確是左對齊的右對齊,而且還多此一舉的把我們的星號*替換成了空格,這有什麼用呢?以我看來,似乎也許大概真的沒什麼用,不知道設計者是怎麼想的。
不過LSet的另一個功能卻是很強大的,可以將一用戶定義類型變數複製到另一用戶自訂類型變數。這又是什麼意思?
還是舉個例子來說明,IP位址知道吧?我在這裡ping百度回傳的IP是61.135.169.125,這種格式的IP位址只是用來給人類看的,IP在電腦內部其實是用32位元整數來表示。如何用VB將xxx.xxx.xxx.xxx格式的IP位址轉成32位元整數形式?一番Google之後,可以寫出類似這樣的程式碼:
複製代碼代碼如下:
Sub Main()
Debug.Print IPToLong("61.135.169.125")
End Sub
Private Function IPToLong(IPStr As String) As Long
Dim Str() As String, HEXStr As String, TempStr As String
Dim x As Long
Str = Split(IPStr, ".")
HEXStr = ""
For x = 0 To UBound(Str)
TempStr = Hex(Str(x))
HEXStr = HEXStr & String(2 - Len(TempStr), "0") & TempStr
Next x
IPToLong = CLng("&H" & HEXStr)
End Function
程式碼可以正常運作,這沒什麼問題,不過我們可以用LSet語句寫出更「高級」的程式碼:
複製代碼代碼如下:
Private Type myBytes
B1 As Byte
B2 As Byte
B3 As Byte
B4 As Byte
End Type
Private Type myLong
Val As Long
End Type
'By Demon
'http://VeVB.COm
Public Function IP2Long(ip As String) As Long
Dim a() As String
Dim b As myBytes
Dim l As myLong
a = Split(ip, ".")
'注意Little-Endian
b.B1 = CByte(a(3))
b.B2 = CByte(a(2))
b.B3 = CByte(a(1))
b.B4 = CByte(a(0))
LSet l = b
IP2Long = l.Val
End Function
用LSet將myBytes類型的變數複製到myLong類型的變量,很好很強大。看一下產生的彙編程式碼:
複製代碼代碼如下:
00401A0E lea eax, dword ptr [ebp-0x20] ; 變數b的位址
00401A11 push eax
00401A12 lea eax, dword ptr [ebp-0x14] ; 變數l的位址
00401A15 push eax
00401A16 push 0x4
00401A18 call __vbaCopyBytes ; jmp to MSVBVM60.__vbaCopyBytes
呼叫的是MSVBVM60.DLL中的__vbaCopyBytes,第一個參數是需要複製的字節,第二個參數是目標位址,第三個參數是來源位址,與C標準函式庫中的memcpy函數類似,只不過參數的順序不一樣,其內部實作無非就是組譯中的串傳送指令:
複製代碼代碼如下:
72A1A0F3 > mov ecx, dword ptr [esp+0x4]
72A1A0F7 push esi
72A1A0F8 mov esi, dword ptr [esp+0x10]
72A1A0FC push edi
72A1A0FD mov edi, dword ptr [esp+0x10]
72A1A101 mov eax, ecx
72A1A103 mov edx, edi
72A1A105 shr ecx, 0x2
72A1A108 rep movs dword ptr es:[edi], dword ptr [esi]
72A1A10A mov ecx, eax
72A1A10C mov eax, edx
72A1A10E and ecx, 0x3
72A1A111 rep movs byte ptr es:[edi], byte ptr [esi]
72A1A113 pop edi
72A1A114 pop esi
72A1A115 retn 0xC
需要注意的是文檔中警告我們:
複製代碼代碼如下:
Warning Using LSet to copy a variable of one user-defined type into a variable of a different user-defined type is not recommended. Copying data of one data type into space reserved for a different data type cantable ca untype.
When you copy a variable from one user-defined type to another, the binary data from one variable is copied into the memory space of the other, without regard for the data types specified for the elements.
用LSet複製使用者定義類型變數是不提倡的,這可能導致預料之外的結果(例如結構沒有對齊),所以,除非你知道自己在做什麼,否則不要使用LSet語句。