Solved

Function Call

Posted on 2000-05-03
5
180 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:Crono15M
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
 
LVL 2

Expert Comment

by:Sage020999
Comment Utility
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
 
LVL 14

Accepted Solution

by:
mcrider earned 100 total points
Comment Utility
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
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now