Solved

Passing an ASP.NET Page object  by value or reference

Posted on 2011-09-23
5
235 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 50
Eagerly loading related objects in entity framework 5 23
Get Top Visible Row of Datagridview 6 26
Exit the loop 4 22
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

19 Experts available now in Live!

Get 1:1 Help Now