By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,201 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,201 IT Pros & Developers. It's quick & easy.

Passing methods arrays of numbers

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 McGuire wrote:
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
Nov 20 '05 #2

P: n/a
* co***********@lycos.co.uk (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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.