VB.Net Tutorial/Class Module/Pass by Reference — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:55, 26 мая 2010

Demonstrates passing by reference.

Module Tester
   Sub Main()
      Dim number1 As Integer = 2
      Console.WriteLine("Before calling SquareByValue, " & "number1 is {0}", number1)
      SquareByValue(number1)  " passes number1 by value
      Console.WriteLine("After returning from SquareByValue, " & "number1 is {0}" & vbCrLf, number1)
      Dim number2 As Integer = 2
      Console.WriteLine("Before calling SquareByReference, " & "number2 is {0}", number2)
      SquareByReference(number2) " passes number2 by reference
      Console.WriteLine("After returning from " & _
         "SquareByReference, number2 is {0}" & vbCrLf, number2)
      Dim number3 As Integer = 2
      Console.WriteLine("Before calling SquareByReference " & "using parentheses, number3 is {0}",number3)
      SquareByReference((number3)) " passes number3 by value
      Console.WriteLine("After returning from " & "SquareByReference, number3 is {0}", number3)
   End Sub
   Sub SquareByValue(ByVal number As Integer)
      Console.WriteLine("After entering SquareByValue, " & "number is {0}", number)
      number *= number
      Console.WriteLine("Before exiting SquareByValue, " & "number is {0}", number)
   End Sub
   Sub SquareByReference(ByRef number As Integer)
      Console.WriteLine("After entering SquareByReference" & ", number is {0}", number)
      number *= number
      Console.WriteLine("Before exiting SquareByReference" & ", number is {0}", number)
   End Sub 
End Module
Before calling SquareByValue, number1 is 2
After entering SquareByValue, number is 2
Before exiting SquareByValue, number is 4
After returning from SquareByValue, number1 is 2
Before calling SquareByReference, number2 is 2
After entering SquareByReference, number is 2
Before exiting SquareByReference, number is 4
After returning from SquareByReference, number2 is 4
Before calling SquareByReference using parentheses, number3 is 2
After entering SquareByReference, number is 2
Before exiting SquareByReference, number is 4
After returning from SquareByReference, number3 is 2

Pass class by reference

Option Strict On
Public Class YourClass
   Private yourName As String
   Private yourNumber As Decimal
   Public Sub New(breed As String)
      yourName = breed
   End Sub
   Public ReadOnly Property Name() As String
      Get
         Return yourName
      End Get
   End Property
   Public Property Number() As Decimal
      Get
         Return yourNumber
      End Get
      Set
         yourNumber = CDec(value)
      End Set
   End Property
   Public Sub ShowInfo()
      Console.WriteLine("This " & yourName & " weighs " & yourNumber & " pounds.")
   End Sub
End Class
Public Class Tester
   Public Shared Sub Main()
      Dim mal As New YourClass("A")
      mal.Number = 130
      ChangeYourClassInfo(mal)
      mal.ShowInfo
      CompletelyChangeYourClassInfo(mal)
      mal.ShowInfo
   End Sub
   Public Shared Sub ChangeYourClassInfo(ByVal aYourClass As YourClass)
      aYourClass.Number = 125
   End Sub
   Public Shared Sub CompletelyChangeYourClassInfo(ByVal aYourClass As YourClass)
      Dim newf As New YourClass("Newfoundland")
      aYourClass = newf
   End Sub
End Class
This A weighs 125 pounds.
This A weighs 125 pounds.

Pass Decimal by reference

Option Strict On
Public Module Tester
   Public Sub Main()
      Dim salary As Decimal = 400.00d
      Dim reviewPts As Integer = 12
      ReceiveRaise(salary, reviewPts)
      Console.WriteLine(salary)
   End Sub
   Function ReceiveRaise(ByRef salary As Decimal, ByVal reviewPts As Integer) _
                         As Boolean
         Dim increasePct As Decimal
         increasePct = ((reviewPts - 10) * .005d)
         salary = salary * (1 + increasePct)
         Return True
   End Function
End Module
404.000

Pass value by reference

Option Strict On
 Imports System
 Module Module1
    Sub Main( )
       Dim theVariable As Integer = 5
       Console.WriteLine("In Run. theVariable: {0}",theVariable)
       Doubler(theVariable)
       Console.WriteLine("Back in Run. theVariable: {0}",theVariable)
    End Sub
    Public Sub Doubler(ByVal param As Integer)
       Console.WriteLine("In Method1. Received param: {0}",param)
       param *= 2
       Console.WriteLine("Updated param. Returning new value: {0}", _
       param)
    End Sub

 End Module
In Run. theVariable: 5
In Method1. Received param: 5
Updated param. Returning new value: 10
Back in Run. theVariable: 5