Solved

VB.NET Implementing Me.Parent property on class.

Posted on 2006-11-30
5
2,172 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 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 63

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 63

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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