Obtain reference to Parent object during instantiation

I have a situation where a custom class is a property of another class.

What I need to be able to do, if it is possible at all, is obtain a reverse to the “parent” class (ie the the class that holds the current class as a property).

For Instance:

Public Class Class1
  ...
  public readonly property Prop11 as Class2
  public property Prop12 as String
  ...
End Class

Public Class Class2
  ...
  private _par as Class1
  private _var21 as string
  ...
  Public Sub New(...)
    me._par = ????
    ...
  End Sub
  public readonly property Prop21 as string
    Get
      return me._par.Prop12 & me._var21
    End Get
  End Property
  ...
End Class

Ultimately, I am trying to access other properties within Class1 from Class2 as they do have substance for information from within Class2. There are several other classes within Class1 that provide descriptive information to other classes contained within it as properties but the information is not extensible to all of the classes through Inheritance, as Class1 is being used as a resource bin for the property classes and the application itself.

Diagram, lazy design ;):

Application <- Class1.Prop12
Application <- Class1.Prop11.Prop21

Question:

Is it possible to get a recursion through this design setup?

3

Well, your design has problems, that’s certain, and you’ve obfuscated the problem a bit too much here.

What I can suggest, based on what you’ve given, is that, instead of this:

class1.Prop12.Prop21

do this:

class1.WrappedProp

where Class1 implements WrappedProp like this:

Public Class Class1
    ...
    Public readonly property WrappedProp as string
        Get
            return me.Prop12 & myChild.Var21
        End Get
    End Property
    ...
End Class

That is to say that the “application” (or whatever object holds a reference to your class1/parent) should not access the child class (or, know about it at all for that matter). From what I can tell, what you’re really doing is providing some kind of container or wrapper with your parent class, but not completely wrapping or containing the child type.

1

I think your design is not optimal, that something is amiss. And the names you are using do not help either.

It’s kind of a forced analogy but… Does a clock need to know who owns it to do its job? Does a pen need to know who owns it to its job? It’s the same thing here. You can say that the clock does need some info about the owner to do its job, specifically the time for the alarm.

But this is not the case here. In your code, Class2 needs something from Class1 in order to even exist. One of its properties can’t leave on it owns without Class1. If it was a method, I’d understand. But a property?

Anyway, alternatives:

  • If you do need to know the owner, pass it as constructor parameter.

  • Pass what the other classes need as parameter.

  • If it is a lot of information, then your Class1 is probably a composition of smaller objects. Break it and pass the little pieces as parameters to other class.

  • Use a 3rd class as a Helper. It’ll use info from the other two classes to do what it’s needed.

PS: If you could edit and make the name of classes/properties more meaningful, we can help you better.

The easiest way to do that is have Class1.Prop21 control the lifetime of Class2, this will allow it to assign the Parent relationship.

2

Well…I would suggest you consider re-thinking your design, but sure you can have two objects that each reference each other; that is trivial.

The real question is how do you want to handle the instantiation / initialization. Does one of the objects, the “parent” as you’ve opted to think about it, contain the “child”, or does a third object get involved to coordinate the aggregation of “parent” and “child”.

However, before you rush into this, a similar concept you might want to check out is the Visitor pattern, which is used for bigger things than this but in a granular sense hinges around one object being passed to another which then passes itself back to that object.

There is also Kent Beck’s “self delegation” pattern; if instead of using properties to couple the two objects together you restructured your logic into one or more methods, you could have the “parent” pass itself into the “child”‘s method(s) as a parameter as needed, thus avoiding back-coupling from the “child” to the “parent”.

Going a little further out, depending on what this “parent” and “child” set up is meant to model, you might just be looking at a Composite implementation.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật