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

Sub1(A$)

and declare it as

Sub1(Int1, Int2, String1)

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

Any help is appreciated
Crono15MAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mcriderConnect With a Mentor Commented:
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$



Cheers!®©
0
 
AzraSoundCommented:
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
0
 
Sage020999Commented:
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
0
 
AzraSoundCommented:
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)
  Next
End Function

that should print each element of your array
0
 
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
Else
   I1 = Val(Left(S,I))
End If

I = I + 1
J = InStr(I, S, " ")
If J = 0 Then
   ' handle bad data here
   Exit Sub
Else
   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)
   Else
      S = ""
   End If
Else
   ' 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.