Passing methods arrays of numbers

C

Colin McGuire

Hi everyone. This is a question and something new and neat I have
found. But to recap - a few months back I learnt about overloading,
how to create the same method with a different signature. With some
help from people in this newsgroup I created a new class with
overloaded constructors that accepted one or two parameters. The code
is below.


Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub
End Class


I have come back to this code now and want to modify it to accept any
number of parameters. I don't want to write dozens of constructors,
one for 3 parameters, one for 4 parameters, one for 5 parameters ....
etc.

I have found this thing called ParamArray. I can modify the class
'test' above to be


Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub

Public Sub New(ByVal ParamArray paramlist() As Integer)
'do something
End Sub

End Class

I can then create an instance of this class by any of

Dim a1 As New test(1)
Dim a2 As New test(1, 2)
Dim a3 As New test(1, 2, 3)
Dim a4 As New test(1, 2, 3, 4)
Dim a5 As New test(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


My question is what if I want to pass the constructor an array of
numbers such as arrayofnumbers.
Dim arrayofnumbers() As Integer = {1, 2, 3, 4, 5}

It seems that I can do it too
dim a6 As New test(arrayofnumbers)

I have defined the argument in the constructor to be a ParamArray and
I can pass in an array. But the reverse is not true and I am not sure
why. For example if my class is

Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub

Public Sub New(ByVal paramlist() As Integer)
'do something
End Sub

End Class

I can create an instance of the class with

Dim a1 As New test(1)
Dim a2 As New test(1, 2)
dim a6 As New test(arrayofnumbers)

But none of the others. How come?

Regards
Colin
 
O

One Handed Man [ OHM# ]

This is because the compiler treats ParamArray in a special way when it is
referenced in Parameters in a function or sub. With the ParamArray, the
parameter does not have to be there whereas, with an Array of strings for
example it does.

In other words, by using the word ParamArray, it tells the compiler to
expect Or Not, an Array of the type specified. Whereas when you use an
ordinary array, it must appear in the signature when called as just that an
array reference not a list of values.

Regards - OHM



Colin said:
Hi everyone. This is a question and something new and neat I have
found. But to recap - a few months back I learnt about overloading,
how to create the same method with a different signature. With some
help from people in this newsgroup I created a new class with
overloaded constructors that accepted one or two parameters. The code
is below.


Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub
End Class


I have come back to this code now and want to modify it to accept any
number of parameters. I don't want to write dozens of constructors,
one for 3 parameters, one for 4 parameters, one for 5 parameters ....
etc.

I have found this thing called ParamArray. I can modify the class
'test' above to be


Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub

Public Sub New(ByVal ParamArray paramlist() As Integer)
'do something
End Sub

End Class

I can then create an instance of this class by any of

Dim a1 As New test(1)
Dim a2 As New test(1, 2)
Dim a3 As New test(1, 2, 3)
Dim a4 As New test(1, 2, 3, 4)
Dim a5 As New test(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


My question is what if I want to pass the constructor an array of
numbers such as arrayofnumbers.
Dim arrayofnumbers() As Integer = {1, 2, 3, 4, 5}

It seems that I can do it too
dim a6 As New test(arrayofnumbers)

I have defined the argument in the constructor to be a ParamArray and
I can pass in an array. But the reverse is not true and I am not sure
why. For example if my class is

Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub

Public Sub New(ByVal paramlist() As Integer)
'do something
End Sub

End Class

I can create an instance of the class with

Dim a1 As New test(1)
Dim a2 As New test(1, 2)
dim a6 As New test(arrayofnumbers)

But none of the others. How come?

Regards
Colin

Regards - OHM# OneHandedMan{at}BTInternet{dot}com
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Colin McGuire) scripsit:
Hi everyone. This is a question and something new and neat I have
found. But to recap - a few months back I learnt about overloading,
how to create the same method with a different signature. With some
help from people in this newsgroup I created a new class with
overloaded constructors that accepted one or two parameters. The code
is below.


Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub
End Class


I have come back to this code now and want to modify it to accept any
number of parameters. I don't want to write dozens of constructors,
one for 3 parameters, one for 4 parameters, one for 5 parameters ....
etc.

I have found this thing called ParamArray. I can modify the class
'test' above to be


Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub

Public Sub New(ByVal ParamArray paramlist() As Integer)
'do something
End Sub

End Class

I can then create an instance of this class by any of

Dim a1 As New test(1)
Dim a2 As New test(1, 2)
Dim a3 As New test(1, 2, 3)
Dim a4 As New test(1, 2, 3, 4)
Dim a5 As New test(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


My question is what if I want to pass the constructor an array of
numbers such as arrayofnumbers.
Dim arrayofnumbers() As Integer = {1, 2, 3, 4, 5}

It seems that I can do it too
dim a6 As New test(arrayofnumbers)

I have defined the argument in the constructor to be a ParamArray and
I can pass in an array. But the reverse is not true and I am not sure
why. For example if my class is

Private Class test
Public Sub New(ByVal param1 As Integer)
'do something
End Sub
Public Sub New(ByVal param1 As Integer, ByVal param2 As Integer)
'do something
End Sub

Public Sub New(ByVal paramlist() As Integer)
'do something
End Sub

End Class

I can create an instance of the class with

Dim a1 As New test(1)
Dim a2 As New test(1, 2)
dim a6 As New test(arrayofnumbers)

But none of the others. How come?

That's the difference 'ParamArray' makes: 'ParamArray' tells VB.NET,
that an array is expected, but it tells VB.NET too, that the array can
be passed as a list of parameters. Without the 'ParamArray', this
doesn't work. Nevertheless, you can use something like that:

\\\
.... As New Test(New Integer() {10, 20, 30})
///

.... with any number of elements in the array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top