Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.NET Implementing Me.Parent property on class.

Posted on 2006-11-30
5
Medium Priority
?
2,224 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
[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
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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