Solved

Function Call

Posted on 2000-05-03
5
184 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
ID: 2775285
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
ID: 2775286
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
ID: 2775305
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
ID: 2775313
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
ID: 2775339
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article will show, step by step, how to integrate R code into a R Sweave document
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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