?
Solved

Passing an ASP.NET Page object  by value or reference

Posted on 2011-09-23
5
Medium Priority
?
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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