VB.Net – Sub过程(子程序)

正如我们在上一章中提到的,Sub过程是不返回任何值的过程。 我们在所有的例子中一直使用Sub过程Main。 到目前为止,我们已经在这些教程中编写控制台应用程序。 当这些应用程序开始时,控制转到主子程序,它反过来运行构成程序主体的任何其他语句。

定义子过程

Sub语句用于声明子过程的名称,参数和主体。 Sub语句的语法是:

[Modifiers] Sub SubName [(ParameterList)] 
    [Statements]
End Sub
  • Modifiers 修饰符 :指定过程的访问级别;可能的值有:Public, Private, Protected, Friend, Protected Friend and information regarding overloading, overriding, sharing, and shadowing.
  • SubName 子名 :表示该子的名字
  • ParameterList 参数列表 :指定参数的列表

示例

以下示例演示了子过程CalculatePay,它接受两个参数小时和工资,并显示员工的总工资:

   Sub CalculatePay(ByRef hours As Double, ByRef wage As Decimal)
      local variable declaration
      Dim pay As Double
      pay = hours * wage
      Console.WriteLine("Total Pay: {0:C}", pay)
   End Sub
   Sub Main()
      calling the CalculatePay Sub Procedure
      CalculatePay(25, 10)
      CalculatePay(40, 20)
      CalculatePay(30, 27.5)
      Console.ReadLine()
   End Sub
End Module

当上述代码被编译和执行时,它产生了以下结果:

Total Pay: $250.00
Total Pay: $800.00
Total Pay: $825.00

按值传递参数

这是将参数传递给方法的默认机制。 在这种机制中,当调用方法时,为每个值参数创建一个新的存储位置。 实际参数的值被复制到它们中。 因此,对方法中的参数所做的更改对参数没有影响。

在VB.Net中,可以使用ByVal关键字声明引用参数。 下面的例子演示了这个概念:

Module paramByval
   Sub swap(ByVal x As Integer, ByVal y As Integer)
      Dim temp As Integer
      temp = x  save the value of x 
      x = y     put y into x 
      y = temp put temp into y 
   End Sub
   Sub Main()
       local variable definition 
      Dim a As Integer = 100
      Dim b As Integer = 200
      Console.WriteLine("Before swap, value of a : {0}", a)
      Console.WriteLine("Before swap, value of b : {0}", b)
       calling a function to swap the values 
      swap(a, b)
      Console.WriteLine("After swap, value of a : {0}", a)
      Console.WriteLine("After swap, value of b : {0}", b)
      Console.ReadLine()
   End Sub
End Module

当上述代码被编译和执行时,它产生了以下结果:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200

它表明,虽然它们在函数内部已更改,但值中没有变化。

通过引用传递参数

引用参数是对变量的存储器位置的引用。 当您通过引用传递参数时,与值参数不同,不会为这些参数创建新的存储位置。 参考参数表示与提供给该方法的实际参数相同的存储器位置。

在VB.Net中,可以使用ByRef关键字声明引用参数。 以下示例演示了这一点:

Module paramByref
   Sub swap(ByRef x As Integer, ByRef y As Integer)
      Dim temp As Integer
      temp = x  save the value of x 
      x = y     put y into x 
      y = temp put temp into y 
   End Sub
   Sub Main()
       local variable definition 
      Dim a As Integer = 100
      Dim b As Integer = 200
      Console.WriteLine("Before swap, value of a : {0}", a)
      Console.WriteLine("Before swap, value of b : {0}", b)
       calling a function to swap the values 
      swap(a, b)
      Console.WriteLine("After swap, value of a : {0}", a)
      Console.WriteLine("After swap, value of b : {0}", b)
      Console.ReadLine()
   End Sub
End Module

当上述代码被编译和执行时,它产生了以下结果:

Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 200
After swap, value of b : 100

作者:andy,如若转载,请注明出处:https://www.web176.com/vbnet/11340.html

(0)
打赏 支付宝 支付宝 微信 微信
andy的头像andy
上一篇 2023年3月1日
下一篇 2023年3月1日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注