Solved

VB.NET Implementing Me.Parent property on class.

Posted on 2006-11-30
5
2,125 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 500 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 62

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 62

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.

743 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

13 Experts available now in Live!

Get 1:1 Help Now