473,395 Members | 1,869 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,395 software developers and data experts.

Creating array on the fly as a parameter to a subroutine?

Consider the following example class:

Public Class ExampleClass
ReadOnly Property HelloWorld() As String
Get
Return "Hello, World"
End Get
End Property
End Class

And a subroutine:

Sub DoSomething(ec as ExampleClass)
End Sub

You can invoke this using:

DoSomething(New ExampleClass)

In that you don't need to create the parameter via variable. Is the same
possible with arrays as parameters:

Sub DoSomething(Words() As String)
End Sub

This can be invoked like this:

Dim MyWords() = {"Hello", "World", "Goodbye"}
DoSomething MyWords

However, is it possible to create an array on the fly like with something
like this:

DoSomething(New String()={"Hello","World","Goodbye"})

This doesn't work but gives an idea.

Cheers, Rob.

PS. I know about ParamArray Words() As String.
Nov 21 '05 #1
4 7471
> DoSomething(New String()={"Hello","World","Goodbye"})

Later...

Hmm, just done some experiments with ParamArray and discovered an
interesting side effect:

Dim MyWords() As String = {"Hello", "World", "Goodbye"}
Something(MyWords)

Sub Something(ByVal ParamArray MyWords() As String)
Dim x As String
For Each Word As String In MyWords
x &= Word & " "
Next
End Sub

The thing that surprised me about this is that I kind of expected VB.NET to
end up with MyWords() inside the sub containing just one item but this item
was itself an array. I have vague memories of this happening in VB6.

However, something different happens in that although only one parameter is
being passed, ParamArray MyWords() As String ends up containing 3 items
which is fine. So it means that when you use ParamArray you can either pass
a series of strings (in this example) or a single string array.

Taking it further:

Something(MyWords, "Wibble", "Wobble") generates a compile error complaining
that:

c:\inetpub\wwwroot\Hops\WebForm1.aspx.vb(32): Value of type '1-dimensional
array of String' cannot be converted to 'String'.

Which is kind of expected.

I assume this is intentional and programmed this way and not an accident of
some kind.

Cheers, Rob.

PS. Doesn't answer my original question though of whether you can create and
initialise arrays on the fly.
Nov 21 '05 #2

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:Op**************@TK2MSFTNGP14.phx.gbl...
Consider the following example class:

Public Class ExampleClass
ReadOnly Property HelloWorld() As String
Get
Return "Hello, World"
End Get
End Property
End Class

And a subroutine:

Sub DoSomething(ec as ExampleClass)
End Sub

You can invoke this using:

DoSomething(New ExampleClass)

In that you don't need to create the parameter via variable. Is the same
possible with arrays as parameters:

Sub DoSomething(Words() As String)
End Sub

This can be invoked like this:

Dim MyWords() = {"Hello", "World", "Goodbye"}
DoSomething MyWords

However, is it possible to create an array on the fly like with something
like this:

DoSomething(New String()={"Hello","World","Goodbye"})

This doesn't work but gives an idea.

Cheers, Rob.

PS. I know about ParamArray Words() As String.

Nov 21 '05 #3
> DoSomething(New String()={"Hello","World","Goodbye"})

Oh, I was so close :-) The syntax is actually:

DoSomething(New String() {"Hello", "World", "Goodbye"})

Didn't need the "=" character which you have to use when initialising a
variable:

Cheers, Rob.
Nov 21 '05 #4
Sub DoSomething(Words() As String)

End Sub

DoSomething(New String(){"Hello", "World", "Goodbye"})
hope that helps..
Imran.

Consider the following example class:

Public Class ExampleClass
ReadOnly Property HelloWorld() As String
Get
Return "Hello, World"
End Get
End Property
End Class
And a subroutine:

Sub DoSomething(ec as ExampleClass)
End Sub
You can invoke this using:

DoSomething(New ExampleClass)

In that you don't need to create the parameter via variable. Is the
same possible with arrays as parameters:

Sub DoSomething(Words() As String)
End Sub
This can be invoked like this:

Dim MyWords() = {"Hello", "World", "Goodbye"}
DoSomething MyWords
However, is it possible to create an array on the fly like with
something like this:

DoSomething(New String()={"Hello","World","Goodbye"})

This doesn't work but gives an idea.

Cheers, Rob.

PS. I know about ParamArray Words() As String.


Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hon Seng Phuah | last post by:
Hi, I am quite new to perl development and have a question very stupid question. I hope that someone can help me. Does perl support in/out parameter subroutine? Like in C/C++, we have a...
6
by: Dylan Nicholson | last post by:
Is there any way of declaring the parameter "array" below to be const, so that the function as written will not compile: void foo(int array /*const*/) { array = array; // should not be allowed...
5
by: - | last post by:
since mysql's stored procedure does not accept an array as a parameter, is it a good practice to input a string delimited parameter eg. call stored_procedure(param1, param2,...
1
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
3
by: TC | last post by:
I just upgraded some code to VB 2005. When I did so, Visual Studio generated several warnings telling me about things which weren't quite perfect in my code. I resolved all the warnings except one....
7
by: steveyjg | last post by:
This is my first post so I'm not sure if this should be in the .NET forum or the VB forum. Anyway I'm using VB .NET and trying to call the Percentile function. The function needs an Object as a...
2
by: twinklyblue | last post by:
Hi team, I am creating a function that would accept an array of integer as its parameter. I have a table where one of its column is declared as an array. This is an excerpt from the script that...
3
by: lukasz.f24 | last post by:
Hello, I'm trying to pass array as an argument into PL/SQL procedure. According to cursor manual (http://cx-oracle.sourceforge.net/html/ cursorobj.html) arrayvar() should be use to do it. I've...
6
by: johntology | last post by:
Hello, I've made a two dimensional array using references, which I gather is the only way to do it in Perl. I now need to pass each interior array to a subroutine for processing and can't quite...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.