Casting in VB


I am new to VB but I know C and, in a VB program I would like to "cast" a function parameter, just like it is done in C. The case is the following :

I have a VB function :
Function MyFunc (ByVal name As String) As Integer

and I want to call this function with an array of bytes as actual value of 'name' parameter without copying it to an intermediary string variable (this is important) :
Dim MyName(10) as Byte

' Fill MyName array with values
MyName(0) = ...

' Call MyFunc with MyName variable
result = MyFunc(MyName)

I guess I am missing here a conversion function taking an array of bytes as parameter and returning a string. Does such function exist in VB ?

Supplementary question : On Windows NT, are VB strings Unicode ?

Who is Participating?
corvanderlindenConnect With a Mentor Commented:
Instead of:

Function MyFunc (ByVal name As String) As Integer


Function MyFunc (ByVal name As Variant) As Integer

Use TypeOf(name) to check for string or array

In VB6 strings are Unicode

'Fill MyName array with values
MyName = name

Sorry, the trick is not to dimension your array:
Dim MyName() As Byte
Dim cArray(260) As Byte
                    Dim sString As String
                    Dim lVal As Long
                    lVal = SendMessage(Me.hwnd, WM_GETTEXT, 255&, cArray(0))

                    sString = StrConv(cArray, vbUnicode)
                    sString = Left(sString, lVal)
                    Debug.Print sString

I believe you will need the c null character at the end of your array.
Im not sure what the actual question is, but I can explain a little more, what caraf_C was talking about

If you do not dimension the array, then you can redimension the number of items in it, i.e.

dim cArray() as byte
redim cArray(255)

This would make the array 255 characters.

If you wanted to do this for a string of an unknown length, you could do the following:

Dim cArray
dim i as integer
for i = 1 to len(string)
    ReDim Preserve cArray(i)
    cArray(i) = mid$(String, i, 1)
next i

Redim Preserve will ReDimension the array, while keeping the contents intact

You would have to pass the array as a variant, which can still be used as an array within the function. Youw would just have to modify the above code to make the array back into a string.

All Courses

From novice to tech pro — start learning today.