Magical *
In the process of using VB programming, I don't think people will not use a string variable, of course, I have not folded. And define a string in VB has the following two methods:
1, DIM S as String
2, DIM S AS STRING * 100
The first method I think is the most used, not to mention here, because the "magical *" of the title of the article, so I want to say a second statement. The second method I want to do the VB program, I don't know what this means, is a string of a length of 100. Here * is indicating a string that declares a fixed length.
In most VB books, talking here is just an explanation of the * number indicating that it is a fixed length string, it doesn't show it to grow, no more other, and more books will talk about a little, say the growing characters The string can contain approximately 2.1 billion (2) characters, and the fixed length string can contain 1 to about 64K (16th party) characters, which is the deepest book I have seen. In the general use, there is no difference in these two methods, as long as the length is sufficient, both can be. But what I want to say is here, this * is sometimes amazing! Because the string declared with the second declaration method, it reflects its magic when it is used as a parameter. At the time of application, sometimes you need to reference Byref (in C / C is a pointer), if you still have a catastrophic memory error in the first declaration method! And use the second method, you can wind calm, everything is normal!
Speaking here, I want to see the magic of *, that is, it can be used as a pointer. Of course, there is no pointer in VB, and the top is quoted (do not say that there is the strPtr, Objptr function to take the address, is not?). Therefore, this problem is often encountered when VB and VC are mixed, because the use of the pointer is inevitably used to deal with the VC, and the VC must be used to know that there are many LPTSTR, LPSTR, and the like. To correspond to such statements in VB, it is best to use the second method so that you can save a lot of trouble.
The following is a simple and common example. Read the string in the INI file in the VB program. Of course, the Windows API has a ready-made function: getPrivateProfileString, and the statement taken in Visual Studio is like this:
Private (Public) Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Ok, now INI file is defined as follows:
[TEST]
Str = pursuer
Let's read the contents of the STR, this segment can be easily set! code show as below
DIM LPSTRING AS STRING * 10
GetPrivateProfileString "TEST", "STR1", "", LPSTRING, 10, "C: /T.ini" msgbox lpstring
This is then this if this:
DIM LPSTRING AS STRING *
GetPrivateProfileString "Test", "STR1", ", LPSTRING, 10," C: / Tini "
Msgbox lpstring
The result ... haha, you try it yourself!
At this time, Gates' operating system will say that your program is very rude, telling you in a very rude way, the program is wrong! Haha ... ..
This is just a simple example. If you use VB and VC hybrid development, such as VC do DLL files in VB, this can solve many problems and very convenient. For example, an existing VC DLL (Handle.dll) file, which has a function of its prototype:
DWORD GETUSERNAME (LPTSTSTSTSTSTSTSR LPUSERNAMEBUF)
This function is used to take the username, and the username will be taken in the lpuserNameBuff and return the number of bytes taken. To use this function in VB, first declare:
Public Declare Function GetUserName Lib "Handle.dll" (Byval LPUSERNAMEBUFF AS STRING) AS Long
Ok, you can use this:
DIM LPUSERNAME AS STRING * 16
Getusername LPUSERNAME
MSGBOX LPUSERNAME
Similarly, if you are:
Dim lpusername as string
GetUserName (lpusername)
MSGBOX LPUSERNAME
........
But you must be your own! Haha ....
Ok, is this * is amazing? Try it!