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
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