?
Solved

VB.NET Implementing Me.Parent property on class.

Posted on 2006-11-30
5
Medium Priority
?
2,261 Views
Last Modified: 2009-07-29
From a user control we can use the me.Parent property to access the properties and methods of the form which holds the user control.

Can anyone give an example of how this could be done on our own classes? I know we could just have a Parent property of the object and pass a reference of the parent into the constructor, however this would create a copy of the parent object into a private variable within the child. Is there a way to only hold a reference to the parent, then when using any of its functions etc use the actual parent object?
0
Comment
Question by:David_Reid_1985
5 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 2000 total points
ID: 18047050
If you pass the Parent by reference, it will store the address of the Parent itself, not a copy.  So in the constructor of the child class, just use ByRef:

Public Sub New(ByRef MyParent As ParentClass)
 ...
End Sub
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 18047196
It actually wouldn't even have to be ByRef.  .NET, like Java, ALWAYS treats objects as references unless you explicitly make a copy (through some form of .Clone() method that'd have to be class-specific)

ByVal vs. ByRef deals, much more annoyingly, with the pointer variables themselves, not the objects to which they point.  So, ByRef is relevant only if you intend to write a line like the following:

Public Sub New(ByRef MyParent As ParentClass)
    MyParent = New ParentClass() ' Replacing what you were given
End Sub

If you're instead doing the more common...
Public Sub New(ByRef MyParent As ParentClass)
    Me.Parent = MyParent
    Me.Parent.CallSomeMethodThatChangesState()
    Me.Parent.Variable = 3
    ' et cetera
End Sub

... then it can go either way.


The moral is: no matter what you do, passing an object into a function does not implicitly copy the object.  Only C++ does that, and that gave everybody a headache anyway.  (-:
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18047268
Hi David_Reid_1985;

When you pass a Object such as a class it alway gets passed as the address of the object and not a copy of the Object.

    Dim obj As MyParent

    Public Sub New( ByVal par As MyClass)
        obj = par
    End Sub

In the above code the class that is creating this class is passing its address to it and that reference is being stored in the variable obj. Obj has access to the original class and a copy was NOT passed in.

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18047336
Hi David_Reid_1985;

To carry on what VoteyDisciple has stated it is safer to always pass ByVal then it is to pass ByRef unless you intend to change the object being passed in, in both the called method and calling method which will destroy the original unless some other variable has a reference to it.

Fernando
0
 

Author Comment

by:David_Reid_1985
ID: 18075312
Oh!

Should have really tried it out before assuming it would not work.

Thanks for your help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 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