VB.Net/Language Basics/ByVal — различия между версиями

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

Версия 16:40, 26 мая 2010

Array parameter passed by Value and by Reference

Imports System
public class MainClass
   Shared Sub Main()
        Dim A As Integer() = {1, 2, 3, 4, 5}
        Dim B As Integer()
        B = A
        Console.WriteLine("Initial state ArrayTests")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("Array A:")
        DisplayArray(A)
        Console.WriteLine("Array B")
        DisplayArray(B)
        FunctionPassArrayByReference1(B)
        Console.WriteLine("After FunctionPassArrayByReference1")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("Array A:")
        DisplayArray(A)
        Console.WriteLine("Array B")
        DisplayArray(B)
        A(0) = 1
        B = A
        FunctionPassArrayByReference2(B)
        Console.WriteLine("After FunctionPassArrayByReference2")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("Array A:")
        DisplayArray(A)
        Console.WriteLine("Array B")
        DisplayArray(B)
        A(0) = 1
        B = A
        FunctionPassArrayByValue1(B)
        Console.WriteLine("After FunctionPassArrayByValue1")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("Array A:")
        DisplayArray(A)
        Console.WriteLine("Array B")
        DisplayArray(B)
        A(0) = 1
        B = A
        FunctionPassArrayByValue2(B)
        Console.WriteLine("After FunctionPassArrayByValue2")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("Array A:")
        DisplayArray(A)
        Console.WriteLine("Array B")
        DisplayArray(B)

   End Sub
   Shared Public Sub FunctionPassArrayByReference1(ByRef Y As Integer())
        Y(0) = 5
    End Sub
   Shared Public Sub FunctionPassArrayByReference2(ByRef Y As Integer())
        Dim NewArray As Integer() = {2, 3, 4, 5, 6}
        Y = NewArray
    End Sub
   Shared Public Sub FunctionPassArrayByValue1(ByVal Y As Integer())
        Y(0) = 5
    End Sub
   Shared Public Sub FunctionPassArrayByValue2(ByVal Y As Integer())
        Dim NewArray As Integer() = {2, 3, 4, 5, 6}
        Y = NewArray
    End Sub
   Shared Public Sub DisplayArray(ByVal Y As Integer())
        Dim X As Integer
        For X = 0 To UBound(Y)
            Console.Write(Y(X).ToString)
            If X <> UBound(Y) Then
                Console.Write(", ")
            Else
                Console.WriteLine()
            End If
        Next
    End Sub
End Class


Array passed By Value

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim i As Integer
      Dim firstArray As Integer()
      Dim firstArrayCopy As Integer()
      firstArray = New Integer() {1, 2, 3}
      firstArrayCopy = firstArray
      Console.WriteLine("Test passing array reference using ByVal.")
      Console.WriteLine("Contents of firstArray before calling FirstDouble: ")
      For i = 0 To firstArray.GetUpperBound(0)
         Console.WriteLine(firstArray(i))
      Next
      " pass firstArray using ByVal
      FirstDouble(firstArray)
      Console.WriteLine("Contents of firstArray after calling FirstDouble: ")
      " print contents of firstArray
      For i = 0 To firstArray.GetUpperBound(0)
         Console.WriteLine(firstArray(i) & " ")
      Next
      " test whether reference was changed by FirstDouble
      If firstArray Is firstArrayCopy Then
         Console.WriteLine("The references are equal.")
      Else
         Console.WriteLine("The references are not equal.")
      End If

    End Sub
   " procedure modifies elements of array and assigns 
   " new reference (note ByVal)
   Shared Sub FirstDouble(ByVal array As Integer())
      Dim i As Integer
      " double each element value
      For i = 0 To array.GetUpperBound(0)
         array(i) *= 2
      Next
      " create new reference, assign it to array
      array = New Integer() {11, 12, 13}
   End Sub
End Class


Function with pass by value Parameter

Imports System
Public Class MainClass
  Shared Sub Main()
    Dim A() As String = {"HELLO", "GOODBYE"}
    Console.WriteLine("Original first item in array is: " _
      & A(0))
    Console.WriteLine("Original second item in array is: " _
      & A(1))
    AFunction(A)
    Console.WriteLine("After passing by value first item in array now is: " _
      & A(0))
    Console.WriteLine("After passing by value second item in array is: " _
      & A(1))
  End Sub
  Shared Sub AFunction(ByVal Foo As String())
    Foo(0) = "GOODBYE"
    Foo(1) = "HELLO"
  End Sub
End Class


Modify Array Element By Value

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim array1 As Integer() = New Integer() {1, 2, 3, 4, 5}
      Dim i As Integer
      Console.WriteLine( "The values of the original array are:" & vbCrLf )
      " display original elements of array1
      For i = 0 To array1.GetUpperBound(0)
         Console.WriteLine(  "  " & array1(i) )
      Next
      Console.WriteLine(  vbCrLf & vbCrLf & _
         "EFFECTS OF PASSING ARRAY ELEMENT " & _
         "BY VALUE:" & vbCrLf & vbCrLf & "array1(3) " & _
         "before ModifyElementByVal: " & array1(3) )
      " array element passed by value
      ModifyElementByVal(array1(3))

    End Sub
   Shared Sub ModifyElementByVal(ByVal element As Integer)
      Console.WriteLine(  vbCrLf & "Value received in " & _
         "ModifyElementByVal: " & element )
      element *= 2
      Console.WriteLine(  vbCrLf & "Value calculated in " & _
         "ModifyElementByVal: " & element )
   End Sub " ModifyElementByVal
End Class


Object parameter passed by Value and by Reference

Imports System
public class MainClass
   Shared Sub Main()
        Dim A As New MyObject()
        Dim B As MyObject = A
        A.X = 1
        Console.WriteLine("Initial state")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        FunctionPassObjectByReference1(B)
        Console.WriteLine("After FobjByRef1")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        A.X = 1
        B = A
        FunctionPassObjectByReference2(B)
        Console.WriteLine("After FobjByRef2")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        A.X = 1
        B = A
        FunctionPassObjectByValue1(B)
        Console.WriteLine("After FobjByVal1")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        A.X = 1
        B = A
        FunctionPassObjectByValue2(B)
        Console.WriteLine("After FobjByVal2")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        
   End Sub
   Shared Public Sub FunctionPassObjectByReference1(ByRef Y As MyObject)
        Y.X = 5
    End Sub
   Shared Public Sub FunctionPassObjectByReference2(ByRef Y As MyObject)
        Y = New MyObject()
        Y.X = 5
    End Sub
   Shared Public Sub FunctionPassObjectByValue1(ByVal Y As MyObject)
        Y.X = 5
    End Sub
   Shared Public Sub FunctionPassObjectByValue2(ByVal Y As MyObject)
        Y = New MyObject()
        Y.X = 5
    End Sub
    Class MyObject
        Public X As Integer
    End Class
End Class


Passing arrays and individual array elements to procedures

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim array1 As Integer() = New Integer() {1, 2, 3, 4, 5}
      Dim i As Integer
      Console.WriteLine( "The values of the original array are:" & vbCrLf )
      " display original elements of array1
      For i = 0 To array1.GetUpperBound(0)
         Console.WriteLine(  "  " & array1(i) )
      Next
      ModifyArray(array1) " array is passed by reference
      Console.WriteLine(  vbCrLf & _
         "The values of the modified array are:" & vbCrLf )
      " display modified elements of array1
      For i = 0 To array1.GetUpperBound(0)
         Console.WriteLine("  " & array1(i) )
      Next
    End Sub
   " procedure modifies array it receives (note ByVal)
   Shared Sub ModifyArray(ByVal arrayParameter As Integer())
      Dim j As Integer
      For j = 0 To arrayParameter.GetUpperBound(0)
         arrayParameter(j) *= 2
      Next
   End Sub " ModifyArray

End Class


Squares three values ByVal and ByRef, displays results

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim number1 As Integer = 2
      Console.WriteLine("Passing a value-type argument by value:")
      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("Passing a value-type argument" & _
         " by reference:")
      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("Passing a value-type argument" & _
         " by reference, but in parentheses:")
      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

   " squares number by value (note ByVal keyword)
   Shared 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 " SquareByValue
   " squares number by reference (note ByRef keyword)
   Shared 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 " SquareByReference
End Class


String parameter passed by Value and by Reference

Imports System
public class MainClass
   Shared Sub Main()
        Dim A As String = "Hello"
        Dim B As String
        B = A
        Console.WriteLine(Chr(10) + "Initial state StringTests")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A: " + A + " B: " + B)
        FunctionPassStringByReference1(B)
        Console.WriteLine("After FunctionPassStringByReference1")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A: " + A + " B: " + B)
        A = "Hello"
        B = A
        FunctionPassStringByReference2(B)
        Console.WriteLine("After FunctionPassStringByReference2")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A: " + A + " B: " + B)
        A = "Hello"
        B = A
        FunctionPassStringByReference3(B)
        Console.WriteLine("After FunctionPassStringByReference3")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("Are A and B equal? " + (A = B).ToString())
        Console.WriteLine("A: " + A + " B: " + B)
        A = "Hello"
        B = A
        FunctionPassStringByValue1(B)
        Console.WriteLine("After FunctionPassStringByValue1")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A: " + A + " B: " + B)
        A = "Hello"
        B = A
        FunctionPassStringByValue2(B)
        Console.WriteLine("After FunctionPassStringByValue2")
        Console.WriteLine("Are A and B the same? " + (A Is B).ToString())
        Console.WriteLine("A: " + A + " B: " + B)
   End Sub
   Shared Public Sub FunctionPassStringByReference1(ByRef Y As String)
        Mid$(Y, 1, 1) = "A"
    End Sub
   Shared  Public Sub FunctionPassStringByReference2(ByRef Y As String)
        Y = "Hello"
    End Sub
   Shared  Public Sub FunctionPassStringByReference3(ByRef Y As String)
        Mid$(Y, 1, 1) = "H"
    End Sub
   Shared  Public Sub FunctionPassStringByValue1(ByVal Y As String)
        Mid$(Y, 1, 1) = "A"
    End Sub
   Shared  Public Sub FunctionPassStringByValue2(ByVal Y As String)
        Y = "Hello"
    End Sub
End Class


Structure parameter passed by Value and by Reference

Imports System
public class MainClass
   Shared Sub Main()
        Dim A As MyStruct
        Dim B As MyStruct
        A.X = 1
        B = A
        Console.WriteLine("Initial state StructTests")
        Console.WriteLine("Are A and B the same? " + (A.Equals(B)).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        FunctionPassStructureByReference1(B)
        Console.WriteLine("After FunctionPassStructureByReference1")
        Console.WriteLine("Are A and B the same? " + (A.Equals(B)).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        A.X = 1
        B = A
        FunctionPassStructureByReference2(B)
        Console.WriteLine("After FunctionPassStructureByReference2")
        Console.WriteLine("Are A and B the same? " + (A.Equals(B)).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        A.X = 1
        B = A
        FunctionPassStructureByValue(B)
        Console.WriteLine("After FunctionPassStructureByValue")
        Console.WriteLine("Are A and B the same? " + (A.Equals(B)).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        A.X = 1
        B = A
        FStructByVal2(B)
        Console.WriteLine("After FStructByVal2")
        Console.WriteLine("Are A and B the same? " + (A.Equals(B)).ToString())
        Console.WriteLine("A.x: " + A.X.ToString() + " B.x " + B.X.ToString())
        
   End Sub
   Shared Public Sub FunctionPassStructureByReference1(ByRef Y As MyStruct)
        Y.X = 5
    End Sub
   Shared Public Sub FunctionPassStructureByReference2(ByRef Y As MyStruct)
        Y = New MyStruct()
        Y.X = 5
    End Sub
   Shared Public Sub FunctionPassStructureByValue(ByVal Y As MyStruct)
        Y.X = 5
    End Sub
   Shared Public Sub FStructByVal2(ByVal Y As MyStruct)
        Y = New MyStruct()
        Y.X = 5
    End Sub

    Structure MyStruct
        Public X As Integer
    End Structure
End Class