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

How return an array value in a function

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
4 40778
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
16
by: priya | last post by:
Hi all, I am new to this group.I am working in c language.I have dount in pointer? how to retun array of pointer in function? example main()
2
by: kathy | last post by:
how to return array from function?
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
10
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
8
by: a | last post by:
Hello. Suppose I have a family of functions f_0(x) = 0*x f_1(x) = 1*x .... f_n(x) = n*x taking float and returning float (naturally, the actual functions would
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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.