What is * 128 in Dim statement?

  • Thread starter Thread starter Suh, Suk Ho
  • Start date Start date
S

Suh, Suk Ho

Dear Colleagues;

I have a very basic question. What does '* 128' means under this context?

Public ProfileString As String * 128

Thank you

Suk Ho
 
Suk Ho,

It means that you are defining a 128-byte string, reserving that amount of
memory. If you just define as string, it will allocate memory as it grows,
which may be quite inefficient.

Try this simple test and see what you get

Dim a As String
MsgBox Len(a)
a = "abc"
MsgBox Len(a)
a = "abcdef"
MsgBox Len(a)

Then repeat it with
Dim a As String * 128
 
I think it's like saying that the string variable will have 128
characters long. Strings variables can hold up to 256 characters but
it's deliminting its storage to 128 characters. Mostly used when
getting information from API's.

Regards
 
Luis Carrion said:
I think it's like saying that the string variable will have 128
characters long. Strings variables can hold up to 256 characters but
it's deliminting its storage to 128 characters. Mostly used when
getting information from API's.

The string size limit is much much greater. I thought it was 32k or
even 64k but it seems to be a bit bigger.

You can put this code into a spreadsheet to verify this:

Private Sub cmdTest_Click()
Dim strText As String, strMsg$
Dim i As Long, x&, strErr$

On Error GoTo LocalErr

For i = 1 To 66000
x = i Mod 10
strText = strText & CStr(x)
If i = 32000 Then
strMsg = "Length of strText: " & CStr(Len(strText)) & vbCrLf & _
"i = " & CStr(i)
MsgBox strMsg
End If
Next i
strMsg = "Length of strText: " & CStr(Len(strText)) & vbCrLf & _
"i = " & CStr(i)
MsgBox strMsg
Exit Sub
LocalErr:
MsgBox i
Err.Clear
End Sub

The most you can type into a line at a time is maybe 256 characters.

But you can always do strText = strText & "Another 256 characters"
to get more text into the string.
 
Hi Jim,

Internally the length of a VBA string is held as a 4 byte integer (a VBA
Long). Thus the maximum length of a string should be 2 ^ 32 (i.e. 4GB).
That said I have a suspicion it might be stored as a signed integer which
would halve the maximum length to 2 ^ 31.

Regards,

Peter Beach
 
Back
Top