Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Functions: Passing by value and by reference

2009-10-10

This is really one of the simplest concepts ever, but since this is something that you need to know as well as something you can benefit from knowing, I think it can be worth a post. Consider this this code:

Class MyProgram

    Shared Sub Main()
        Dim X As Integer = 10
        DoSomething(X)
        Console.WriteLine(X)
    End Sub

    Private Shared Sub DoSomething(ByVal X As Integer)
        X += 1
    End Sub

End Class

The output you get here is of course 10. X in DoSomething is different from X is Main, and the variable X in DoSomething is just initialized with the value 10, and then it is increased by one to 11 and thereafter it is destroyed, leaving X in main with the value 10. This is because the parameter is passed by value (ByVal).

If I was to change the parameter in DoSomething to be passed by reference instead, as in the following code, the output would be 11.

Class MyProgram

    Shared Sub Main()
        Dim X As Integer = 10
        DoSomething(X)
        Console.WriteLine(X)
    End Sub

    Private Shared Sub DoSomething(ByRef X As Integer)
        X += 1
    End Sub

End Class

This is because DoSomething no longer holds an own integer variable, just a reference to the variable that is passed by the caller, Main. When X in Main is passed to DoSomething, and DoSomething is manipulating that value, the value is changed.

When I work with objects, the rules are the same, but the effect is different. If I was to pass a bitmap to a function, and the function then is manipulated, the original bitmap will be affected no matter if it is passed by value or by reference (ByRef). This is because the value of the bitmap object variable is a reference to the actual object, and a reference to a object variable also is a reference to the same actual object.

Consider this code:

Class MyProgram

    Shared Sub Main()
        Dim B As New System.Drawing.Bitmap(100, 100)
        DoSomething(B)
        Console.WriteLine(B.Width.ToString())
    End Sub

    Private Shared Sub DoSomething(ByVal B As System.Drawing.Image)
        B = New System.Drawing.Bitmap(200, 200)
    End Sub

End Class

The output will be 100, because when you change the value of an object variable (the value of an object variable is the reference to the actual object) the passing variable is not affected, because the code holds two variables: B in Main and B in DoSomething. Changing the value of B in DoSomething will not affect B in Main.

Changing the ByVal in DoSomething to ByRef, would give an output of 200, since the code then only would contain one variable: B in Main. DoSomething will simply refer to that variable.

Categories: Visual Basic 9

Tags: Functions

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips