473,378 Members | 1,447 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,378 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 40770
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.