Results 1 to 6 of 6

Thread: passing ParamArray to another ParamArray

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    21

    Question passing ParamArray to another ParamArray

    I am using the VBScript control, I have to pass multiple uncertain parameters to the VBScript control.

    'my publich business method which is called by many business components:
    Public Sub RunTheScript(ByVal s As String, ParamArray items())
    'some biz routines here to manipulate items before passing to the script control

    ScriptControl.Run(s, items)
    End Sub

    'the fixed Run method of VBScript control
    Private Sub Run(ByVal ProcedureName As String, ParamArray items())
    End Sub


    'a biz caller
    RunTheScript "foo",1,2,3,"a","b","c"

    as you can see, I cannot change the behavior of the VBScript control, the items of RunTheScript pass to the ScriptControl.Run, will turn to be an array of array, which is:

    items(0)(0) = 1
    items(0)(1) = 2
    items(0)(2) = 3
    items(0)(3) = "a"
    items(0)(4) = "b'
    items(0)(5) = "c"


    that is NOT what I want, I want is the normal result as we all expected:

    items(0) = 1
    items(1) = 2
    items(2) = 3
    items(3) = "a"
    items(4) = "b'
    items(5) = "c"


    Anyone could solve the problem?

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: passing ParamArray to another ParamArray

    ParamArray and normal Array are two different things.
    With Sub Run(ByVal ProcedureName As String, ParamArray items()), ParamArray items() will be passed as a sequence of parameters of Variant but not an array of parameters.
    When call ScriptControl.Run(s, items), you have passed items as one and only one value (a whole array) as the first and single item of ParamArray items() of Sub Run().

    Try to change ParamArray in Sub Run() to a normal Array like this:
    Code:
    Private Sub Run(ByVal ProcedureName As String, items())
       '...
    End Sub
    If that single change does not work then try the code below to convert ParamArray items() to normal Array arItems():
    Code:
    'my publich business method which is called by many business components:
    Public Sub RunTheScript(ByVal s As String, ParamArray items())
    'some biz routines here to manipulate items before passing to the script control
       Dim i As Integer, arItems()
       
       ReDim arItems(LBound(items) to UBound(items))
       For i = LBound(items) to UBound(items)
          arItems(i) = items(i)
       Next
       ScriptControl.Run(s, arItems)
    
    End Sub
    
    'the fixed Run method of VBScript control
    Private Sub Run(ByVal ProcedureName As String, items())
       '...
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: passing ParamArray to another ParamArray

    Since you cannot change the Run procedure but still need to pass each element of the ParamArray as a separate item, figure out what is most likely the maximum number of items in the ParamArray. Add 25 or 50% to that value to allow for future expansion and then use something like the following.

    Code:
    Public Sub RunTheScript(ByVal s As String, ParamArray items())
        Select Case Ubound(items)
            Case 0
              ScriptControl.Run(s, items(0))
            Case 1
              ScriptControl.Run(s, items(0), items(1) )
            Case 2
              ScriptControl.Run(s, items(0), items(1), items(3) )
            '...more cases
            Case Else
                msgbox "Param Array contains too many items."
        End Select
    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    21

    Re: passing ParamArray to another ParamArray

    hi, that's what I am working around. but I think this is really not a good solution.

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: passing ParamArray to another ParamArray

    but I think this is really not a good solution.
    but it is probably your only option.

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: passing ParamArray to another ParamArray


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