• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Function Call

I have a string coming in via winsock.
Call it A$.

That string is of the format,
    "$int int stringVbCrLf"
Example:  "$1 1 String1||" The || representing the VbCrLf.

What I need to do is insert those three values as parameters of a function.
Does it work to do the following?
Call the function (subroutine) as


and declare it as

Sub1(Int1, Int2, String1)

the other problem is that it is space delimited, not comma delimited.

Any help is appreciated
1 Solution
do you have vb6? if so use the split function on the string, it will break it up into three elements of an array for you, then you can just pass that single array as a parameter
You can not pass A$ in as an integer, this should give you a file type mismatch.  Can you change Sub1 to read a string and sub Call Sub1("A$",,)?

If you are using VB6 you can use the split command to parse your string.  This will allow you to pass an array if you like.

Hope this helps
Coding Sub1 this way will allow you to access the variables inside the subroutine like you want:

    Sub Sub1(Source As String)
        Dim Int1 As Integer
        Dim Int2 As Integer
        Dim String1 As String
        Dim iVal As Long
        Dim jVal As Long
        iVal = InStr(1, Source, " ")
        jVal = InStr(iVal + 1, Source, " ")
        Int1 = CInt(Left$(Source, iVal))
        Int2 = CInt(Mid$(Source, iVal, jVal - iVal))
        String1 = Mid$(Source, jVal + 1)
        ' Int1 Int2 and String1 now contain the proper values
        ' The rest of your code goes here
        ' The debug lines below just show the variables...
        Debug.Print "Int1 is "; Int1
        Debug.Print "Int2 is "; Int2
        Debug.Print "String1 is "; String1
    End Sub

Then you just call it like this:  

      Sub1 A$

after creating the array just pass it to the function, for example

Private Sub Command1_Click()
  Dim arr As Variant
  arr = Split(A$)
  Call MyFunc(arr)
End Sub

Function Sub1 MyFunc(arr As Variant)
  Dim i As Integer
  For i = 0 To UBound(arr)
    Debug.Print arr(i)
End Function

that should print each element of your array
Brendt HessSenior DBACommented:
Let's assume your string can have embedded spaces.  This means that the Split function won't work.  So, let's parse it.

Sub ParseString(S as String, I1 as Integer, I2 as Integer)

Dim I as Integer, J as Integer
I=InStr(S," ")
If I = 0 Then
   ' handle bad data here
   Exit Sub
   I1 = Val(Left(S,I))
End If

I = I + 1
J = InStr(I, S, " ")
If J = 0 Then
   ' handle bad data here
   Exit Sub
   I2 = Val(Mid(S,I,J))
End If

S = Mid(S,J+1) ' take remainder of string
If Right(S,2) = vbCrLf Then
   If Len(S)>2 Then
      S = Left(S,Len(S)-2)
      S = ""
   End If
   ' handle bad data here
   Exit Sub
End If

End Sub

To use this, you would pass the string variable in the first parameter, and two integer variables as the second and third parameters:

Dim I1 as Integer, I2 as Integer, A as String

ParseString A, I1, I2

Then you can call your subroutine:

Sub1 A, I1, I2

Of course, you can always embed this code in your 'Sub1' and parse it there.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now