LSet statements and RSet statements in VB6 _VB

Source: Internet
Author: User

There are several statements that look very much like VB6: Let, Set, LSet, RSet.

Let is used for the assignment of general variables:

Copy Code code as follows:

[let] varname = expression

Most of the cases we omit let, directly with the equal value, so that many people do not know the existence of let.

Set is used to assign values to objects, to point variables to objects and to increase the reference count of objects, and many people do not know what the reference count is.

So what is LSet for? I look like let and set of the combination, but it is not. The L in LSet is the abbreviation for left, which corresponds to Rlet. You asked me how I knew L was the left abbreviation? The document is written in the chant:

Copy Code code as follows:

LSet Statement

Left aligns a string within a string variable, or copies a variable of one user-defined type to another of a diff Erent user-defined type.

RSet Statement

Right aligns a string within a string variable.


LSet more than RSet a function, first do not look at this, first look at the same part, the two are used in a string variable in a string to the left (right). What does that mean? In fact, I do not understand the document, the actual test is good:
Copy Code code as follows:

Sub Main ()
Dim URL as String
Dim S as String

Let URL = "Http://www.jb51.net"
s = string$ (20, "*")

LSet s = URL
Debug.Print S

RSet s = URL
Debug.Print S
End Sub

Output (note spaces):

Copy Code code as follows:

Http://www.jb51.net

Http://www.jb51.net



It's really right aligned on the left, and it's superfluous to replace our asterisk * with a space, what's the use? In my opinion, it may seem to be really useless, do not know what the designer is thinking.

However, another feature of LSet is powerful, and you can copy a user-defined type variable to another user-defined type variable. What does that mean?

Or an example to illustrate the IP address, you know? Here I ping Baidu return IP is 61.135.169.125, this format IP address is only used for human to see, IP in the computer is actually using 32-bit integer to express. How to use VB to turn the IP address of xxx.xxx.xxx.xxx format into 32-bit integer form? After Google, you can write code similar to this:

Copy Code code as follows:

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

The code works, which is fine, but we can write more "advanced" code with the LSet statement:

Copy Code code as follows:

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://jb51.net

Public Function Ip2long (IP as String) as Long
Dim A () as String
Dim B as Mybytes
Dim L as MyLong

A = Split (IP, ".")
' Pay attention to 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

It's good and powerful to copy mybytes type variables to MyLong type variables with LSet. Look at the generated assembly code:

Copy Code code as follows:

00401A0E Lea eax, DWORD ptr [ebp-0x20]; Address of variable B
00401A11 push EAX
00401a12 Lea eax, DWORD ptr [ebp-0x14]; The address of the variable L
00401A15 push EAX
00401A16 Push 0x4
00401a18 call __vbacopybytes; JMP to Msvbvm60.__vbacopybytes

Called is the __vbacopybytes in MSVBVM60.DLL, the first parameter is the byte that needs to be copied, the second parameter is the destination address, and the third parameter is the source address, similar to the memcpy function in the C standard library, except that the order of the parameters is different. Its internal implementation is nothing more than a string transfer instruction in the assembly:

Copy Code code as follows:

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

Note that the documentation warns US:

Copy Code code as follows:

Warning Using LSet to copy a variable of one user-defined type into a variable of a different user-defined the type is not R Ecommended. Copying data of one data type into spaces reserved for a different data type can cause unpredictable results.

When your copy a variable from one user-defined type to another, the binary data from one variable is copied into the Memor Y space to the other, without regard for the "data types specified for the elements."

It is not recommended to replicate user-defined type variables with LSet, which can result in unexpected results (such as a struct not aligned), so do not use LSet statements unless you know what you are doing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.