VB.Net Tutorial/Class Module/Pass by Value

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

Pass Integer by value to a function

public class Test
   public Shared Sub Main
        Dim myInteger As Integer = 5
        TestByVal(myInteger)
        Console.WriteLine(myInteger)
    End Sub
    Private Shared Sub TestByVal(ByVal int As Integer)
        int = int * 2
    End Sub
End class
5

Pass Single by value and by reference to a function

public class Test
   public Shared Sub Main
        Dim sngFahrenheit As Single
        Dim sngCelsius As Single
        sngCelsius = 12.12
        sngFahrenheit = DegreesByValue(sngCelsius)
        Console.WriteLine(sngCelsius.ToString & " degrees Celsius are " & _
           sngFahrenheit.ToString & " degrees Fahrenheit.")
        sngCelsius = 12.12
        sngFahrenheit = DegreesByReference(sngCelsius)
        Console.WriteLine(sngCelsius.ToString & " degrees Celsius are " & _
           sngFahrenheit.ToString & " degrees Fahrenheit.")
   End Sub
   Shared Function DegreesByValue(ByVal sngCelsius As Single) As Single
        DegreesByValue = (9 / 5) * sngCelsius + 32
        sngCelsius = 0
    End Function
   Shared Function DegreesByReference(ByRef sngCelsius As Single) As Single
        DegreesByReference = (9 / 5) * sngCelsius + 32
        sngCelsius = 0
    End Function
End class
12.12 degrees Celsius are 53.816 degrees Fahrenheit.
0 degrees Celsius are 53.816 degrees Fahrenheit.