Passing an ASP.NET Page object by value or reference

Hi,

I am passing a page object to a classes New subroutine by value but don't really know what is going on behind the scenes. Here is the code...

 
Public Class MyClass
    Public ThePage As Page
    Public Sub New(byVal myPage As Page)
        ThePage = myPage
    End Sub
End Class

Dim objMyClass As New MyClass(Me.Page)

Open in new window


So, have I copied and created a whole new Page object called ThePage, or is there still only one instance of the page except objMyClass.ThePage points to the original Page instance passed in through the NewSub(...).

I don't really want to make another instance of the page, I just want to reference the only instance.

I hope my question makes sense and thanks for your time.
MonCapitanAsked:
Who is Participating?
 
Craig WagnerConnect With a Mentor Software ArchitectCommented:
The only way to create a new object is by calling New.

When you pass an "object" by value you are passing the address of, or a pointer to, the object. In your case you have one Page instance and two pointers to it. Let's say you wrote the following code:

Dim objMyClass As New MyClass(Me.Page)
Me.Page.Title = "My Page"
Dim title As String = objMyClass.ThePage.Title

Open in new window


The variable title would contain "My Page" because Me.Page and objMyClass.ThePage point to exactly the same object.

Having said that, given the code we've seen so far you would get the exact same results if you passed it ByRef. The difference between ByVal and ByRef really only becomes a factor if you create a new instance using the passed variable. Unfortunately your situation doesn't lend itself very well to a good example. Let's say you had the following:

public sub Foo( ByVal myPage As Page)
myPage = new Page()
myPage.Title = "bar"
end sub

Me.Page.Title = "foo"
Foo(Me.Page)
string title = Me.Page.Title

Open in new window


The value of title will be "foo" because you passed a copy of the address to the page. When you overwrote the copy of the address you had two objects in memory, one pointed to by Me.Page and one pointed to by myPage. Now change it to pass ByRef:


public sub Foo( ByRef myPage As Page)
myPage = new Page()
myPage.Title = "bar"
end sub

Me.Page.Title = "foo"
Foo(Me.Page)
string title = Me.Page.Title

Open in new window


Now the value of title will be "bar." You passed the address of the address, so when you said myPage = new Page() you overwrote the old reference.
0
 
MonCapitanAuthor Commented:
That was a really good explanation thank you.

My understanding was that whenever anything is passed ByVal to a function, it creates a copy so that when the function manipulates myPage.Title for instance, Me.Page.Title is not affected. So, if I understand you correctly, if you did this...

 
public sub IncreaseValue(ByVal intValue As Integer)
   intValue += 1
end sub

Dim intOutSideValue As Integer = 5
IncreaseValue(intValue)
dim intResult = intOutsideValue

Open in new window


...so intResult = 5 still because a copy was made when passing it to IncreaseValue. So, if my understanding is correct so far, this rule does not apply to objects. Am I correct in thinking this?

So, how would I code it so that when I do pass in an object, I can manipulate the (copied) objects values so that the outside object is not affected?
0
 
CodeCruiserCommented:
>So, if my understanding is correct so far, this rule does not apply to objects. Am I correct in thinking this?

Correct.

>So, how would I code it so that when I do pass in an object, I can manipulate the (copied) objects values so that the outside object is not affected?

You would need to implement Cloning. Here are some examples

http://www.java2s.com/Code/VB/Class/ClonableObject.htm

http://www.developerfusion.com/code/8063/deep-clone-an-object-in-net/
0
 
Craig WagnerSoftware ArchitectCommented:
The rule does apply to objects, it's just when you pass an object you're actually passing a reference (pointer) to the object. So when you pass a reference (pointer) to an object ByVal you're passing a copy of the reference (pointer). While you can manipulate the properties of the object and see those changes upon return, you cannot manipulate the reference (pointer) itself and see those changes upon return.

As stated, you'd have to make a complete copy (clone) of the existing object if you didn't want the original object affected. In that case you would have two separate objects. I'm not sure why you would want to do that though. In my opinion cloning an object often indicates you're doing something that is the equivalent of trying to fit a square peg in a round hole.
0
 
MonCapitanAuthor Commented:
Yeh I agree about the round whole not fitting an equally sided shape of sorts, but I can think of times when one might use this method but so far, not in any of my projects.

Well this has been a learning experience in that I seem to have been coding correctly by instinct rather than by facts, so,  thank you very much for your clear explanation and time.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.