我可以做:私有函数XX()(ByVal ABC作为字符串)为单个吗?尝试返回XX()... [英] Can I do: Private Function XX() (ByVal ABC As String) As Single ? Trying to return XX() ...

查看:85
本文介绍了我可以做:私有函数XX()(ByVal ABC作为字符串)为单个吗?尝试返回XX()...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,快速提问.我想返回此值:Dim XX()作为Single
从一个功能.但是我还需要将此参数ABC传递给函数.

我在这篇文章的主题中写的方式是错误的,但这就是我正在尝试做的并且不确定如何做.任何建议表示赞赏.

p.s.该函数实际上为数组XX()的每个元素分配值. 该函数实际上为数组XX()中的每个元素分配一个值.因此,返回时,我希望返回整个填充的数组

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

推荐答案

如果您使用Private字,则该函数仅在此模块中可见被定义为.要在其他模块中使用它并获取更多信息,请阅读有关范围 [ ^ ]

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[^]

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

    Return RetVal
End Function



用法:



Usage:

Dim abc As Integer = 5
Dim sngValue As Single = XX(abc)
Msgbox sngValue.ToString()


以下syntax 可以用于return an array of Single values.

array reference type.因此,即使xx是从函数返回的函数内的局部变量,它也会在堆上为reference to the array object提供保持该函数内分配的值的函数.

xx---refers--->array on heap.语句Dim arrayOfElements = GetArrayOfElements(6)使局部变量arrayOfElements---refers--->array on heap,因此根据问题的要求,返回array 以及elements assigned within the function.
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.
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


如果要求在声明中使用该功能from outside the class,则public 或其他适当的访问级别应适用于Function GetArrayOfElements


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


这篇关于我可以做:私有函数XX()(ByVal ABC作为字符串)为单个吗?尝试返回XX()...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆