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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.