Results 1 to 5 of 5

Thread: CallByName with param array

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    CallByName with param array

    How can I simplify this?

    VB Code:
    1. Sub Run(Func As String, Par() As Variant)
    2.     Select Case UBound(Par)
    3.         Case 0: CallByName Me, Func, VbMethod, Par(0)
    4.         Case 1: CallByName Me, Func, VbMethod, Par(0), Par(1)
    5.         Case 2: CallByName Me, Func, VbMethod, Par(0), Par(1), Par(2)
    6.         Case 3: CallByName Me, Func, VbMethod, Par(0), Par(1), Par(2), Par(3)
    7.         '...
    8.     End Select
    9. End Sub

  2. #2

  3. #3

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    I'm heading for the theory, there's yet no usage for this piece of code. The idea is to access any function of a written project without having to write a interface for each. I know about the performance penalties of CallByName.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427
    OK, I guess I don't get it. A typical Sub in a program might look like


    VB Code:
    1. Public Sub MySub(strOne As String, intTwo As Integer)
    and to call it in the normal fashion you would do
    VB Code:
    1. MySub "test", 3

    I guess with your Run sub you would have to do something like this

    VB Code:
    1. Dim AnArray(1) As Variant
    2. AnArray(0) = "test"
    3. AnArray(1) = 2
    4. Run "MySub", AnArray
    and you would have to figure out how to handle the Type Mismatches. I don't mean to be derisive, but it seems silly to go through the trouble.

  5. #5

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Yeah that's right but it's probably better than writing a huge wrapper function to get each and any function in the project.

    Well as said I'm more likely looking for the theory here, I got loads of trouble understanding the ParamArray type. It's basically the same problem as when passing arrays to a ParamArray:

    VB Code:
    1. 'Same code, different header
    2. Public Sub TextOut1(ParamArray List())
    3.     Dim A As Long, Temp As String
    4.     For A = 0 To UBound(List): Temp = Temp & List(A): Next
    5.     MsgBox Temp
    6. End Sub
    7.  
    8. 'Same code, different header
    9. Public Sub TextOut2(List() As Variant)
    10.     Dim A As Long, Temp As String
    11.     For A = 0 To UBound(List): Temp = Temp & List(A): Next
    12.     MsgBox Temp
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16.     Dim Temp() As Variant
    17.    
    18.     ReDim Temp(1)
    19.     Temp(0) = "O"
    20.     Temp(1) = "K"
    21.    
    22.     'Can't swap functions here
    23.     TextOut1 Temp(0), Temp(1)
    24.     TextOut2 Temp
    25. End Sub

    I know it's silly but you can always try to find a way huh?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width