473,691 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 40874
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(arra yTest(), 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******** ********@TK2MSF TNGP12.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******** ********@TK2MSF TNGP12.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
10226
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 it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1 = new Array(); GridArrayName1 = new Array ('test-value'); GridArrayName1 = GridArrayName1...
0
6457
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, 'b_id' => 9, 'c_id' => 3,
4
157991
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
3416
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? /******************************************************** Below is my code: ********************************************************/
16
2485
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
21524
by: kathy | last post by:
how to return array from function?
2
1920
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 The question may be silly or even meaning less but please............
10
5600
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
1734
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
9079
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8951
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8791
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7624
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6457
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4322
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1954
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.