Passing objects in Visual Studio 2003

Ok we know that when passing "byVal" in methods strings,  booleans e.t.c their values do not change, but when passing "ByRef" they do change. However when passing objects it does not matter if we pass it "ByVal" or "ByRef". Either way the object changes!! Lets see that in practice:

Create a Web project and place a button on the form. Then create a class and the following methods:

Public Class Class1
    Private _mytitle As String
    Public _mytitlee As String
    Public Property mytitle() As String
        Get
            Return _mytitle
        End Get
        Set(ByVal Value As String)
            _mytitle = Value
        End Set
    End Property
    Public Sub setTitle(ByVal myob As Class1)
        myob.mytitle = "new title"
    End Sub
    Public Sub setTitlee(ByVal str As String)
        str = "this the new title"
    End Sub
End Class



You see we pass "ByVal" a string and a class. Now let's go to the form. Write the following for the click event of the button:

'the object that we are going to pass
        Dim myobj As New Class1
        '.....
        Dim myclasss As New Class1
        Dim str As String = ""
        myclasss.setTitle(myobj)
        myclasss.setTitlee(str)
        Response.Write(myobj.mytitle)
        Response.Write(str)



Now run the project ... you will see the unexpected. The object does change value allthough it's beeing passed by value...
Share


Έχουν δημοσιευτεί Τετάρτη, 24 Μαΐου 2006 11:01 πμ από το μέλος zeon

Ενημέρωση για Σχόλια

Αν θα θέλατε να λαμβάνετε ένα e-mail όταν γίνονται ανανεώσεις στο περιεχόμενο αυτής της δημοσίευσης, παρακαλούμε γίνετε συνδρομητής εδώ

Παραμείνετε ενήμεροι στα τελευταία σχόλια με την χρήση του αγαπημένου σας RSS Aggregator και συνδρομή στη Τροφοδοσία RSS με σχόλια

Σχόλια:

 

cap έγραψε:

That happens because the "value" passed is essentially a copy of the pointer to the object's memory location. In other words, the object in your example is a "reference type". (FYI, the only types that byval actually creates copies for are value types - integers, booleans, dates - but NOT strings or class types).

So when you pass an object (a reference type) byval, something gets copied - but what gets copied is the pointer to the object, not the object itself. In order to actually copy an object you have to create a deep clone of it - that is, a copy of the object itself and all the objects it contains.

To conclude, we've got value types and reference types. When you pass a reference type, a copy of the type is never made - so there is practically little difference between passing it byval or byref - the object does change if you change it.


Μαΐου 24, 2006 5:48 μμ
 

dt008 έγραψε:

If we pass an object(pointer) by reference,we are then able to change the object that this pointer points to, so that it points to another object in memory. That cannot happen if we pass the pointer by value.
Μαΐου 25, 2006 7:06 μμ
 

Sotiris Filippidis' Weblog έγραψε:

Με αφορμή τη δημοσίευση Passing Objects in Visual Studio 2003 που έγινε στο blog του Zeon, έφτιαξα...
Μαΐου 27, 2006 2:24 πμ
 

Sotiris Filippidis' Weblog έγραψε:

Με αφορμή τη δημοσίευση Passing Objects in Visual Studio 2003 που έγινε στο blog του Zeon, έφτιαξα...
Μαΐου 27, 2006 2:25 πμ
 

Sotiris Filippidis' Weblog έγραψε:

Με αφορμή τη δημοσίευση Passing Objects in Visual Studio 2003 που έγινε στο blog του Zeon , έφτιαξα δύο

Νοεμβρίου 7, 2007 12:37 μμ
 

Elsmani έγραψε:

I think you've just captured the asnewr perfectly

Φεβρουαρίου 27, 2013 7:25 μμ

Ποιά είναι η άποψή σας για την παραπάνω δημοσίευση;

(απαιτούμενο)
απαιτούμενο
προαιρετικό
απαιτούμενο
ÅéóÜãåôå ôïí êùäéêü:
CAPTCHA Image