PC Review


Reply
Thread Tools Rate Thread

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

 
 
Rob Nicholson
Guest
Posts: n/a
 
      30th Mar 2005
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.


 
Reply With Quote
 
 
 
 
Rob Nicholson
Guest
Posts: n/a
 
      30th Mar 2005
> 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.


 
Reply With Quote
 
Rob Nicholson
Guest
Posts: n/a
 
      30th Mar 2005

"Rob Nicholson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Rob Nicholson
Guest
Posts: n/a
 
      30th Mar 2005
> 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.


 
Reply With Quote
 
Imran Koradia
Guest
Posts: n/a
 
      30th Mar 2005
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.
>




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use the value of a parameter passed to a subroutine to update a re David Microsoft Access Form Coding 4 10th Apr 2008 03:48 PM
How to pass a workshhet name as a parameter into a subroutine ? =?Utf-8?B?eWlnYWxi?= Microsoft Excel Misc 4 9th Jan 2005 10:28 AM
Passing a subroutine as a parameter to another subroutine =?Utf-8?B?UmljaGFyZCBHcmFudA==?= Microsoft VB .NET 7 18th Feb 2004 04:09 PM
How to pass a subroutine as a parameter to another subroutine? Richard Microsoft VB .NET 2 15th Jan 2004 07:52 PM
subroutine with fixed list of parameter vipul DotNet Microsoft VB .NET 3 17th Oct 2003 11:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:51 AM.