在 VB.Net 中调用另一个函数中的一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18143380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:36:14  来源:igfitidea点击:

call one function within another function in VB.Net

.netvb.netfunction

提问by user2625447

is there a way to call function within another function eg:

有没有办法在另一个函数中调用函数,例如:

call functionname()

but this can be called only within the sub, is there a round about?, I have tried using GOTO but even that should be within the same sub or function.

但这只能在 sub 中调用,是否有轮回?,我曾尝试使用 GOTO 但即使它也应该在同一个 sub 或函数中。

回答by Adriaan Stander

Well you can do this

那么你可以做到这一点

public void myfunc()
{
}

public void callerOfMyFunc()
{
    myFunc();
}

I think you might want to read up a little about Methods (C# Programming Guide)

我想你可能想读一点关于方法(C# 编程指南)

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it is called by the common language runtime (CLR) when the program is started.

方法是包含一系列语句的代码块。程序通过调用方法并指定任何必需的方法参数来执行语句。在 C# 中,每条执行的指令都是在方法的上下文中执行的。Main 方法是每个 C# 应用程序的入口点,在程序启动时由公共语言运行时 (CLR) 调用。

Even for VB you can look at Sub Procedures (Visual Basic)

即使对于 VB,您也可以查看Sub Procedures (Visual Basic)

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.

Each time the procedure is called, its statements are executed, starting with the first executable statement after the Sub statement and ending with the first End Sub, Exit Sub, or Return statement encountered.

You can define a Sub procedure in modules, classes, and structures. By default, it is Public, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. The term, method, describes a Sub or Function procedure that is accessed from outside its defining module, class, or structure. For more information, see Procedures in Visual Basic.

A Sub procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

Sub 过程是由 Sub 和 End Sub 语句包围的一系列 Visual Basic 语句。Sub 过程执行一个任务,然后将控制权返回给调用代码,但它不向调用代码返回值。

每次调用该过程时,都会执行其语句,从 Sub 语句之后的第一个可执行语句开始,并以遇到的第一个 End Sub、Exit Sub 或 Return 语句结束。

您可以在模块、类和结构中定义 Sub 过程。默认情况下,它是 Public,这意味着您可以从应用程序中可以访问您定义它的模块、类或结构的任何位置调用它。术语方法描述从其定义模块、类或结构外部访问的 Sub 或 Function 过程。有关详细信息,请参见 Visual Basic 中的过程。

Sub 过程可以接受由调用代码传递给它的参数,例如常量、变量或表达式。

回答by Abbas

Public Sub DoSomething
    OtherFunction()
End Sub

Public Sub OtherFunction()
    'Do something here
End Sub

回答by Gavin Sutherland

How about this?

这个怎么样?

Function MethodOne() As Boolean
    Dim result As Boolean = False

    ' Do something 

    Return result
End Function

Function MethodTwo() As Boolean
    ' Call Method One
    Dim res As Boolean = MethodOne()

    Return res
End Function

回答by stupid

Function MethodOne() As Boolean
    Dim result As Boolean = False

    ' Do something 

    Return result
End Function

Function MethodTwo() As Boolean
    ' Call Method One
    Dim res As Boolean = MethodOne()
End Function