[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Passing an ASP.NET Page object  by value or reference

Posted on 2011-09-23
5
Medium Priority
?
252 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

649 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