Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Controls / July 2008



Tip: Looking for answers? Try searching our database.

Problem with ParamArray

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 07 Jul 2008 23:21 GMT
Hi everyone:

I am using VB6, and I have two functions that they basically take the same
arguments.  The number of arguments is indefinite, so I am using the
ParamArray option.  However, when I send argument into a function, and that
function sends it out to another one, the program crashes with the error
"Type mismatch".  Here is the example of my code:

Private Sub Command1_Click()

   MsgBox MainFunc(1, 2, 3, 5, 7, 9)

End Sub

Function MainFunc(ParamArray Arguments())

   y = sum(Arguments)

   .

   .

   .' Calculate Z based on the value of y

   MainFunc = Z

End Function

Function sum(ParamArray Arguments())

   For Each x In Arguments

       y = y + x

   Next x

   sum = y

End Function

Does anyone know how I can fix this, without changing the eliminating either
of the function calls?  Thanks for your help.

Bob
argusy - 08 Jul 2008 02:38 GMT
> Hi everyone:
>
[quoted text clipped - 40 lines]
>
> Bob

I think 'sum' is a reserved word.
Change the fumction to DoSum, or anything else, and see if the problem
disappears
MikeD - 08 Jul 2008 04:50 GMT
> I think 'sum' is a reserved word.

No, it's not.  You might be thinking of SQL or some other language.

This code works fine in VB:

Option Explicit

Private Function sum(a, b)

   sum = a + b

End Function

Private Sub Form_Load()

   Dim a
   Dim b

   a = 5
   b = 10
   MsgBox sum(a, b)

End Sub

(Ignore the use of variants; I just didn't feel like typing data types for
this simple example)

Signature

Mike
Microsoft MVP Visual Basic

argusy - 08 Jul 2008 05:40 GMT
>> I think 'sum' is a reserved word.
>
[quoted text clipped - 23 lines]
> (Ignore the use of variants; I just didn't feel like typing data types for
> this simple example)

You're right, Mike.
I just came back from seeing a doctor, and re-read this.
Yep - I was thinking of Excel. I knew 'sum' came in somewhere, and I
couldn't get it out of my head that it was in VB.
Maybe I should see a doctor more often!!

Graham
MikeD - 08 Jul 2008 04:43 GMT
> Hi everyone:
>
[quoted text clipped - 33 lines]
> Does anyone know how I can fix this, without changing the eliminating
> either of the function calls?  Thanks for your help.

What line does the type mismatch occur on? It might not have anything to do
with the ParamArray.  Your code did not include variable declarations.
Therefore, an incorrect data type for a variable cannot be ruled out. It
would have been better if you had posted the complete code (copied and
pasted) and not shortened/edited it. BTW, you ARE using Option Explicit,
right?

But, just for the hell of it, try calling your sum function as such:

y = sum(Arguments())

Signature

Mike
Microsoft MVP Visual Basic

Bob - 08 Jul 2008 18:47 GMT
Hi Mike:

Thanks for the reply.  First of all, the code that you see is the main part
of my code.  Second, the error occurs on the line y=y+x in the function sum.
I just don't know how to fix this, as the function sum may be called
independently, and the function MainFunc may be called independently.  So,
the same argument goes into both function, and they need to be used with
ParamArray.  Now, one option is to copy the entire code for sum inside
MainFunc every time sum is called.  Of course this is a work around, but it
defeats the purpose of having a function.  Let me know what you think.
Thanks;

Bob

>> Hi everyone:
>>
[quoted text clipped - 44 lines]
>
> y = sum(Arguments())
Sinna - 08 Jul 2008 07:48 GMT
> Hi everyone:
>
[quoted text clipped - 40 lines]
>
> Bob

I've had the same issue a while ago and after some digging I found out
that when passing an argument declared as ParamArray to another routine,
it is passed as an array of variants, no longer as ParamArray.

So when you're passing Arguments() from the MainFunc routine to the sum
routine, you don't pass an ParamArray but an array of Variants. That's
why you get a type mismatch error.

Sinna
Bob - 08 Jul 2008 18:49 GMT
Hi Sinna:

Thanks for your reply.  How did you fix the problem before?  As you see, the
function sum may be called independently, and the function MainFunc may be
called independently.  So, the same argument goes into both function, and
they need to be used with ParamArray.  I sort of replied the same thing to
Mike above.  Thanks for your help.

Bob

>> Hi everyone:
>>
[quoted text clipped - 50 lines]
>
> Sinna
senn - 08 Jul 2008 21:25 GMT
> Hi Sinna:
>
[quoted text clipped - 5 lines]
>
> Bob

Possibly it can't be done unless below.
/senn

Private Sub Command1_Click()
  Dim z
   'MsgBox
   z = MainFunc(1, 2, 3, 5, 7, 9)
End Sub

Function MainFunc(ParamArray Arguments())
   Dim y, z, i
   Dim Ary(5)
   For Each z In Arguments()
      Ary(i) = z
      i = i + 1
   Next
   i = i - 1
   y = sum(Ary(), i)
   MainFunc = y
End Function

Function sum(Arr(), eleSiz)
 Dim y, x
    For x = 0 To eleSiz
      y = y + Arr(x)
   Next x
   sum = y
End Function
senn - 08 Jul 2008 22:26 GMT
>> Hi Sinna:
>>
[quoted text clipped - 5 lines]
>>
>> Bob

Alternate, using UBound and Redim Preserve
not knowing the elementSize.

Private Sub Command1_Click()
  Dim z
   'MsgBox
   z = MainFunc(1, 2, 3, 5, 7, 9)
End Sub

Function MainFunc(ParamArray Arguments())
   Dim y, z, i
   Dim Ary()
   For Each z In Arguments()
      ReDim Preserve Ary(i)
      Ary(i) = z
      i = i + 1
   Next
   i = i - 1
   y = sum(Ary())
   MainFunc = y
End Function

Function sum(Arr())
 Dim y, x
     For x = 0 To UBound(Arr)
      y = y + Arr(x)
   Next x
   sum = y
End Function
Sinna - 09 Jul 2008 07:19 GMT
> Hi Sinna:
>
[quoted text clipped - 5 lines]
>
> Bob

I don't remember very well, but I try to use the ParamArray() as less as
possible (as a result of the 'misbehavior'). When really needed, I check
if the first argument in the ParamArray() is an array. If so, I check if
there are more parameters passed in. If not, I assume that the first
parameter is the ParamArray of the calling function. Using (a slight
modification of) UBound, I parse the array as like it was a ParamArray.

In the meanwhile I found my implementation:

<code>
Public Function FormatStr( _
        ByVal Source As String, ParamArray Arguments()) As String

    Dim pArguments() As Variant, lParamCount As Long

    If Not IsMissing(Arguments) Then
        lParamCount = UBound(Arguments) + 1

        If lParamCount > 0 Then
            If IsArray(Arguments(0)) Then
                pArguments = Arguments(0)
            Else
                pArguments = Arguments
            End If
        Else
            pArguments = Arguments
        End If

        lParamCount = UBound(pArguments) + 1

        'From now on, use pArguments instead of Arguments.
        '...
    End If
End Function
</code>

Sinna
Bob - 11 Jul 2008 17:51 GMT
>> Hi Sinna:
>>
[quoted text clipped - 43 lines]
>
> Sinna
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.