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

How return an array value in a function

P: n/a
How would I pass an array back to a sub routine from a function? That is, I
have a function that looks like this

Public Function arrayTest() As Array
Dim states() As String = { _
"AZ", "CA", "WA" _
}
Return states
End Function

I have a button with code like this:

Dim states() As String
states = arrayTest()

However, I get a squiqqly under the "ArrayTest()" saying , "Option strict on
disallows implicit conversions from System.array to 1-demsional array of
string.

I would like to be able to write the states code once and have it called
from a variety of places. My first thought is to write it in a function,
and call the values when needed from the function. If this is the best way
of doing this, how do I do it and if not, what is better?

Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Woody,
You either need to define your function as returning an array of strings. or
you need to cast the value returned to an array of strings.

Return an array of strings:
Public Function arrayTest() As String()
Dim states() As String = { _
"AZ", "CA", "WA" _
}
Return states
End Function

Cast the value returned: Public Function arrayTest() As Array Dim states() As String
states = directcast(arrayTest(), string())
I prefer the first over the second, however casting the value is useful
also:

Hope this helps
Jay
"Woody Splawn" <wo***@splawns.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... How would I pass an array back to a sub routine from a function? That is, I have a function that looks like this

Public Function arrayTest() As Array
Dim states() As String = { _
"AZ", "CA", "WA" _
}
Return states
End Function

I have a button with code like this:

Dim states() As String
states = arrayTest()

However, I get a squiqqly under the "ArrayTest()" saying , "Option strict on disallows implicit conversions from System.array to 1-demsional array of
string.

I would like to be able to write the states code once and have it called
from a variety of places. My first thought is to write it in a function,
and call the values when needed from the function. If this is the best way of doing this, how do I do it and if not, what is better?

Nov 20 '05 #2

P: n/a
"Woody Splawn" <wo***@splawns.com> scripsit:
How would I pass an array back to a sub routine from a function? That is, I
have a function that looks like this

Public Function arrayTest() As Array
Dim states() As String = { _
"AZ", "CA", "WA" _
}
Return states
End Function

I have a button with code like this:

Dim states() As String
states = arrayTest()

However, I get a squiqqly under the "ArrayTest()" saying , "Option strict on
disallows implicit conversions from System.array to 1-demsional array of
string.


Why not declare the function like this?

\\\
Public Function ArrayTest() As String()
Nov 20 '05 #3

P: n/a
Hi Woody,

The syntax that you are after looks like this:
Public Function States() As String()
Return New String() { "CA", "FL" }
End Function

This creates a new copy of the array for each 'user' that calls the
function. If any of the users 'tampers' with the array it will have no effect
on the next user.
The simplest version
Public Module Things
Public StateCodes() As String = New String() { "CA", "FL" }
End Module

And you can also do this:
Private m_StateCodes() As String = New String() { "CA", "FL" }
Public Function States() As String()
Return m_StateCodes
End Function

These are more efficient in that the array is only created once, but if
any user changes a State's code, this will effect all subsequent users. The
second version adds an interface between the user and the array which would
allow you to change how m_StateCodes is implemented.

Then there's
Private m_StateCodes() As String = New String() { "CA", "FL" }
Public ReadOnly Property States As String()
Get
Return m_StateCodes
End Get
End Function

This one also allows the array to be tampered with. This is despite the
ReadOnly, as that refers to the Property - not the array contents.

Another version:
Private m_StateCodes() As String = New String() { "CA", "FL" }
Public ReadOnly Property StateCode (Index As Integer) As String
Get
Return m_StateCodes (Index) 'Add error checking as required.
End Get
End Function

This will provide the individual states and not allow them to be altered.
This means a Property access for each State code which is less efficient than
a direct array access. However, I believe that the JIT compiler can put the
code for small function/property calls inline which removes the call-return
overhead..

Which is best depends on how you are using this array. If you want
occasional access then I would suggest the last version - a Property for
accessing individual State codes. If you want to process the list of State
code, eg in creating a table or filling a ListBox, etc, then I would suggest
the first version - a tamper-proof function which provides the whole list.

The versions in between are just for background.

Regards,
Fergus
Nov 20 '05 #4

P: n/a
In the VB.Net online documentation, look in UserControl class. One of the
uses for defining a UserControl is: "Another efficient use of the user
control is to simply preload a ComboBox or ListBox with static items you
commonly use in almost every application; some examples of this are
countries/regions, cities, states, and office locations."
Gary
"Woody Splawn" <wo***@splawns.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
How would I pass an array back to a sub routine from a function? That is, I have a function that looks like this

Public Function arrayTest() As Array
Dim states() As String = { _
"AZ", "CA", "WA" _
}
Return states
End Function

I have a button with code like this:

Dim states() As String
states = arrayTest()

However, I get a squiqqly under the "ArrayTest()" saying , "Option strict on disallows implicit conversions from System.array to 1-demsional array of
string.

I would like to be able to write the states code once and have it called
from a variety of places. My first thought is to write it in a function,
and call the values when needed from the function. If this is the best way of doing this, how do I do it and if not, what is better?

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.