Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi folks, quick question. I'm trying to return this: Dim XX() As Single
from a function. But I also need to pass this parameter ABC to the function.

the way I wrote in the subject of this post is wrong, but that's what i'm trying to do and not sure how. any suggestions appreciated.

p.s. The Function actually assigns values to each element of the array XX()
The function actually assigns a value to each element in the array XX() . so upon returning, i'm hoping to return the whole populated array
Posted
Updated 20-Jun-12 4:44am
v4

If you use Private word, the function will be visible only in this module where is defined. To use it in the other modules and to get more informations, read about SCOPE[^]

VB
Function XX() (ByRef ABC As Integer) As Single
Dim RetVal As Single = 0
    
    'do something

    Return RetVal
End Function


Usage:
VB
Dim abc As Integer = 5
Dim sngValue As Single = XX(abc)
Msgbox sngValue.ToString()
 
Share this answer
 
v2
Comments
VJ Reddy 20-Jun-12 12:54pm    
From the description given in the question I think the OP wants to return an array of elements by the Function.
Please see my answer.
Thank you.
Maciej Los 20-Jun-12 15:20pm    
Yes, but the OP changed the question ;) Please, see the first version.
VJ Reddy 20-Jun-12 22:22pm    
Seen the initial version. I think the intent of the question is not clear in the initial version.
Thank you :)
Maciej Los 21-Jun-12 7:46am    
;)
SandiegoSSD 20-Jun-12 13:16pm    
Thanks.5
The following syntax can be used to return an array of Single values.

The array is a reference type. Hence, even though xx is a local variable within the function when it is returned from the function it gives reference to the array object on the heap holding the values assigned within the function.

xx---refers--->array on heap. The statement Dim arrayOfElements = GetArrayOfElements(6) makes the local variable arrayOfElements---refers--->array on heap, so as it is required in the question, the array is returned, with the elements assigned within the function.
VB
Sub Main
    Dim arrayOfElements = GetArrayOfElements(6)
    For each element as Single in arrayOfElements
    	Console.WriteLine(element)
    Next element
End Sub

Private Function GetArrayOfElements(ByVal Abc as integer) as Single()
    'size of xx will be Abc+1 No. of elements
	Dim xx(Abc) as Single
	For I as integer = 1 to Abc
		xx(I) = I * 2
	Next I
	Return xx
End Function

'Output

'0
'2
'4
'6
'8
'10
'12

If the function is required to be used from outside the class in which it is declared, then public or other appropriate access level shall be for Function GetArrayOfElements
 
Share this answer
 
Comments
SandiegoSSD 20-Jun-12 13:17pm    
thank you. I was screwing around and ended up using the same way you suggested.
VJ Reddy 20-Jun-12 13:28pm    
You're welcome and thank you for viewing and accepting the solution :)
Sergey Alexandrovich Kryukov 20-Jun-12 15:08pm    
Right, a 5.
--SA
VJ Reddy 20-Jun-12 22:19pm    
Thank you, SA :)
Maciej Los 20-Jun-12 15:21pm    
Good answer for the latest version of question, my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900