Some experience in VB programming

zhaozj2021-02-17  57

Some experience in VB programming

1. Suppose there is a variable declaration in VB: DIM S1, S2 AS STRING S1 is a variant type variable, while S2 is a string type variable If you want to declare two string variables S1 and S2 should be used: DIM S1 AS String , S2 as string

2. The object in VB is automatically reclaimed. Similar to Java in a process of sub foo () Dim Obj AS New Object .... 'Do Something with Obj End Sub process is not necessary set obj = Nothing Because when you leave When the process is, the local variable OBJ disappears, so the reference to the Object object instance disappears (unless other global variables are referenced in the process), the object instance will be released automatically.

However, for the modular-level object variable, New has been used after an instance, and the instance must be set to Obj = Nothing.

3. Assignment of object variables should be used in this way, which is equivalent to let Obj pointing to AnotherObj. The object variable in VB is substantially a pointer to the object instance, which is the same as Java, Pascal, and C .

4. The internal storage format of strings in VB is Unicode, which can be automatically converted to an ANSI character (single-byte character) or DBCS character (double-byte character). For example, DIM S1 AS STRING, S2 AS STRING S1 = "Chinese" S2 = LEFT (S1, 1) is actually S2 = "in" because VB will automatically use the "in" in the S1 internal Unicode as a DBCS The character is taken out to S2 Therefore, it should be paid to the two-byte characters, it is easy to generate an array overflow error.

Common string processing functions in VB, such as ASC, Left, MID ... will automatically determine that each character in the process is single byte or double bytes (because the string is stored in the interior, so this It is easy to do it) and then automatically convert to an ANSI character or DBCS character.

5. The comparison of the string should be used with strcmp, not simple use = STRComp (String1, String2 [, Compare])

The value of the parameter compare is as follows: Constant value meaning VBuseCompareOption -1 Based on the Option COMPARE statement to make a string comparison VbinaryCompare 0, it is to view the Unicode characters in String1 and String2 as an array for the array. VbTextCompare 1 Perform a text Comparison VBDatabaseCompare 2 This option is only available for Microsoft Access, comparing according to the settings of the database.

When the English string is compared, it is not distinguished by the case, for example: "a" is equal to "a"; when the Chinese or other double-byte string is compared, the full-width character and half-horn characters are not distinguished, for example A "," a "," a "," a "is equal;

6. Functions of string processing in VB have three versions: (1) ANSI and DBCS versions, general string functions (such as MID (), left (), ...) is the version, the version of the function can Automatically identify the ANSI characters and DBCS characters, and whether the ANSI character or DBCS characters are treated as a character processing (although a DBCS character is two bytes, but still as a character processing) (2) Second, this version The function is backed from B, such as MIDB (), leftb () ...; this version of this version is processed by byte array, such as S = "ABC", k = lenb ( s), then k = 6, because the string stores in the VB in Unicode, and a Unicode character accounts for two bytes, so S actually occupies 2 * 3 = 6 bytes of space, so lenb (s) Return 6

(3) Unicode version, this version of the function is after the first class function name plus W, such as ASCW, chrw; this version of the function is handled as Unicode processing.

Functions Function Description Asc Returns the ANSI or DBCS character code for the first character of a string. AscB Returns the value of the first byte in the given string containing binary data. AscW Returns the Unicode character code for the first character of a string. Chr Returns a string containing a specific ANSI or DBCS character code. ChrB Returns a binary string containing a specific byte. ChrW Returns a string containing a specific Unicode character code. Input Returns a specified number of ANSI or DBCS characters from a file. InputB Returns a specified number of bytes from a file. InStr Returns the first occurrence of one string within another. InStrB Returns the first occurrence of a byte in a binary string. left, right Returns a specified number of characters from the right or left sides of a string LEFTB Rightb Returns A Specified Number of bytes from the left or right side of a binary string. Len Returns the length of the string in number of characters. LenB Returns the length of the string in number of bytes. Mid Returns a specified number of characters from a string. MidB Returns the Specified Number of Bytes from a binary string.

7. The following identifiers in the VB program code cannot contain double-byte characters: Public Procedure Names Public Process Name Public Variables Public Variable Public Constants Public Specific Project Name (AS Specified In The Project Properties Dialog Box) Engineering Name (Conversation in Engineering Properties The name in the box) [PS: VB Chinese version of the project name can be Chinese, strange] Class Names (Name Property Of A Class Module, A User Control, A Property Page, OR A User Document) class name ( Class module, user control module, property page, user document Name attribute) In other words, other identifiers can be used in Chinese, for example: private sub cmdtest_click () DIM Chinese Variable AS String Chinese Variable = "Hello! Hello! "Msgbox Chinese variable END SUB

Such a code is also legal :-)

转载请注明原文地址:https://www.9cbs.com/read-30812.html

New Post(0)