Vb.net 如何使用函数return

Vb.net 如何使用函数return,vb.net,function,Vb.net,Function,我想知道这样做是否正确。 我有这个示例代码,它应该表示一个用户操作,并在一个函数中进行计算,然后这个函数应该将值返回给我的sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim i As Integer = 1 Test_function(i) MsgBox(i) End Sub Private Function Test_function(ByV

我想知道这样做是否正确。 我有这个示例代码,它应该表示一个用户操作,并在一个函数中进行计算,然后这个函数应该将值返回给我的sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i As Integer = 1
    Test_function(i)
    MsgBox(i)
End Sub

Private Function Test_function(ByVal i As Integer)
    i = i + 1
    Return (i)
End Function
当我运行这段代码时,我得到:

i=1分接头

在功能上i=2

i=1分


你是如何把i=2加入我的潜艇的?或者这不是正确的使用方法吗?

没什么好奇怪的,这只是一个简单的误解。如果您这样调用,您正在打印
i
的实际值:

MsgBox(Test_function(i))
那么两者都是一样的; 或者,您可以通过以下方式进行检查:

Dim tempVal = Test_function(i)'<-- will give 2
MsgBox(i)'<--- will give 1
相同的函数调用将为您提供两个值,即
2

 Dim tempVal = Test_function(i)'<-- will give 2
 MsgBox(i)'<--- will give 2

Dim tempVal=Test_function(i)”没什么好奇怪的,这只是一个简单的误解。如果您这样调用,您正在打印
i
的实际值:

MsgBox(Test_function(i))
那么两者都是一样的; 或者,您可以通过以下方式进行检查:

Dim tempVal = Test_function(i)'<-- will give 2
MsgBox(i)'<--- will give 1
相同的函数调用将为您提供两个值,即
2

 Dim tempVal = Test_function(i)'<-- will give 2
 MsgBox(i)'<--- will give 2

Dim tempVal=Test_function(i)”我想你要问的是为什么调用Test_function时,
i
没有改变

让我们把你的代码分解一下

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i As Integer = 1 'creating a variable named i, value 1.
    Test_function(i) 'passing the value of the variable i to test function, but not doing anything with what the function returns.
    MsgBox(i) 'calling messagebox with the value i.
End Sub

Private Function Test_function(ByVal i As Integer) 'creating a new variable also named i with the value being passed in.
    i = i + 1 'incrementing the value of the i variable by 1.
    Return (i) 'returning i 
End Function
因此,据我所知,有一些概念您可能误解了-
ByVal
的含义,可能是变量作用域的概念,以及
Return
的作用

显而易见的答案是,您没有使用
Test\u函数返回的值。如果您有
i=test\u函数(i)
,则调用
test\u函数将使
i
增加


另一种方法是传递
i
ByRef而不是ByVal-如果这样做,则
Test\u函数
方法范围内的
i
将与
按钮1\u单击
方法范围内的
i
相同。但是,因为您要将它传递给
ByVal
i
变量实际上是两个完全不同的变量

我想你要问的是,
I
为什么没有被调用Test\u函数所改变

让我们把你的代码分解一下

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i As Integer = 1 'creating a variable named i, value 1.
    Test_function(i) 'passing the value of the variable i to test function, but not doing anything with what the function returns.
    MsgBox(i) 'calling messagebox with the value i.
End Sub

Private Function Test_function(ByVal i As Integer) 'creating a new variable also named i with the value being passed in.
    i = i + 1 'incrementing the value of the i variable by 1.
    Return (i) 'returning i 
End Function
因此,据我所知,有一些概念您可能误解了-
ByVal
的含义,可能是变量作用域的概念,以及
Return
的作用

显而易见的答案是,您没有使用
Test\u函数返回的值。如果您有
i=test\u函数(i)
,则调用
test\u函数将使
i
增加



另一种方法是传递
i
ByRef而不是ByVal-如果这样做,则
Test\u函数
方法范围内的
i
将与
按钮1\u单击
方法范围内的
i
相同。但是,因为您要将它传递给
ByVal
i
变量实际上是两个完全不同的变量

Dim myVar=Test_function(i)
myVar将保存返回的值-您的代码没有任何内容可以捕获/保存/存储返回值您还可以编写“i=Test_function(i)”哦,我明白了,现在开始了!!谢谢你的评论@杰菲,这就像数学中的函数一样
y=f(x)
-->
Dim y As Integer=Test_function(i)
。请帮自己一个忙,通过在代码文件顶部添加
Option Strict on
或在项目属性中设置它来启用Option Strict。
Dim myVar=Test_function(i)
MyVar将保存返回的值-您的代码不需要捕获/保存/存储返回值您还可以编写“i=Test\u function(i)”哦,我明白了,现在就开始吧!!谢谢你的评论@杰菲,这就像数学中的函数一样
y=f(x)
-->
Dim y As Integer=Test_function(i)
。请帮自己一个忙,通过在代码文件顶部添加
Option Strict on
或在项目属性中设置它来启用Option Strict。因此,如果我错了,请纠正我,如果要编辑变量值并返回它,请使用ByRef&使用ByVal使用值但不返回值?这是最好的使用方法吗?不,ByRef vs ByVal只是控制是否传递值或共享引用。如果共享引用,则两个方法将查看相同的内存位置,被调用方法中的修改将反映在调用方法中,而不管是否返回值。如果传递该值,则被调用方法内的修改不会反映在调用方法上。然而,如果您传递的是一个类和一个原语(如整数),那么这可能意味着不同的事情。阅读此->注意,在VB中默认情况下,所有内容都是ByVal,因此
Test_函数(ByVal i为整数)
TestFunction(i为整数)
相同,感谢您的回复!这很有帮助。是否还有返回多个值/引用的方法?返回(i,y)否,您可以传递值
ByRef
,使用
out
参数,或返回自定义类或expandoobject,但vb中没有ruby中的多个返回。因此,如果我错了,请纠正我,如果要编辑变量值并返回它,请使用ByRef&使用ByVal使用值但不返回它?这是最好的使用方法吗?不,ByRef vs ByVal只是控制是否传递值或共享引用。如果共享引用,则两个方法将查看相同的内存位置,被调用方法中的修改将反映在调用方法中,而不管是否返回值。如果传递该值,则被调用方法内的修改不会反映在调用方法上。然而,如果您传递的是一个类和一个原语(如整数),那么这可能意味着不同的事情。阅读此->注意,在VB中默认情况下,所有内容都是ByVal,因此
Test_函数(ByVal i为整数)
TestFunctio相同