What is * 128 in Dim statement?

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
 
B

Bob Phillips

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
 
L

Luis Carrion

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
 
J

Jim Carlock

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.
 
P

Peter Beach

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top