Solved

Passing an ASP.NET Page object  by value or reference

Posted on 2011-09-23
5
233 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:MonCapitan
  • 2
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 36590967
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
 

Author Comment

by:MonCapitan
ID: 36591455
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36592363
>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
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 36592402
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
 

Author Comment

by:MonCapitan
ID: 36593284
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now