Len, LenB Functions |
Len(string | varname)
string
Use: Required
Data Type: String
A valid string expression.
varname
Use: Required
Data Type: Any except object
A valid variable name.
A Long integer
Counts the number of characters within a string or the size of a given variable. Use LenB to determine the actual number of bytes required to hold a given variable in memory.
string and varname are mutually exclusive; that is, you must specify either string or varname, but not both.
If either string or varname contains Null, Len returns Null.
Len returns the size (number of characters) that a user-defined type occupies when written to a file.
LenB returns the actual size of a user-defined type in memory.
When you use LenB with byte data or a Unicode string, LenB returns the number of bytes that represent the data or the string.
You can't use Len with an object variable.
If varname is an array, you must also specify a valid subscript. In other words, Len can't determine the total number of elements in or the total size of an array.
When you use a random access file to store data and a user-defined type to handle that data within your application, you can use Len to determine the value of the Len = clause of the file's Open statement. However, if you have used variable length strings within your user-defined type, Len may not accurately determine the actual storage requirement of the user-defined type. For this purpose, fixed-length strings that are set equal to the maximum size of the string field should be used instead. The following example shows how to use the Len function to specify the buffer length when opening a random access file:
Option Explicit Type udtTest FName As String * 20 LName As String * 25 Age As Integer End Type Public udtRec(1 To 10) As udtTest Public Function RandomFileSave() As Boolean Dim sFile As String Dim iFile As Integer Dim i As Integer sFile = "test.dat" iFile = FreeFile Open sFile For Random As #iFile Len = Len(udtRec(1)) For i = 1 To 10 Put #iFile, i, udtRec(i) Next i Close #iFile End Function
Variants are treated the same as a string variables, and Len returns the actual number of characters stored to the variable. But this can lead to unexpected results. Take the following snippet as an example:
Dim vVar vVar = 100 MsgBox Len(vVar)
You may expect the Len function to return 2 because iVar is obviously a variant of subtype integer. In fact, Len returns 3—the number of characters contained within the variant.
When used with a strongly typed variable, Len returns the number of bytes required to store that variable. The length of a Long, for instance, is 4.
Because Visual Basic uses Unicode strings (which store each character in two bytes) internally, different return values are obtained from Len and LenB when string variables are passed. For example, a string of four characters returns 4 from Len, but returns 8 from LenB.
Just in case you had any doubt about the efficiency of explicitly declaring data types wherever possible, you can try this quick example with the LenB function:
Dim lVar As Long Dim vVar lVar = 10000000 vVar = 10000000 MsgBox "The Long version uses " & LenB(lVar) & _ " bytes of memory" & vbCrLf & _ "The Variant version uses " & LenB(vVar)
The conclusion is clear: variants consume significantly more memory than strongly typed variables.
3.144.227.251