473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Passing methods arrays of numbers

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
2 1465
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
6
by: Raistlin | last post by:
Hey, for c++ does any body have a way to compare two arrays? Say one has the numbers 1-5000, the other has *most* of the numbers from 1-5000, is there a way to display the numbers that are in the...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
2
by: John Spiegel | last post by:
Hi all, Any suggestions on the best way to pass a set of values to a stored procedure that will use them in a query? Specifically, I want to call a stored procedure from .NET that takes a set...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
0
by: volx | last post by:
Hello all: What is the proper way to implement in MC++ a web service which accepts a multi dimensional array as a parameter to one of its methods? This does compile: double...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
3
by: vajratkarviraj | last post by:
hey guys is it possible 2 do so??? as in like i want 2 write numbers from 1 to 1000 in a file f.txt say.... using the while loop im stuck... as the indexing variable for incrementing the numbers to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.