473,671 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Whats the best way for a child class to know about its parent class

I'm using WPF and c#. Whats the best way for a child class to know about
it's parent class? For examle

class ParentClass : CollectionBase
{
// code...

class ChildClass
{
// code...

void SomeMethod()
{
if(myParentClas s.Count>0)
{
//do something
}
}

}
}

Thanks.

--
mo*******@noema il.noemail
Mar 2 '07 #1
2 1241
Hi moondaddy,

The ChildClass will not have any reference to the ParentClass. Consider
this: an instance of ChildClass could be added to multiple ParentClass.

If you could guarantee an instance of ChildClass could only be added to one
ParentClass. You could create a public property in the ChildClass to hold
the reference to ParentClass and let the ParentClass.Add or Insert to set
this reference.

class ChildClass
{
private ParentClass _parent;

internal ParentClass Parent
{
get { return _parent; }
set { _parent = value; }
}

}

class ParentClass : CollectionBase
{
public int Add(ChildClass item)
{
if (item != null) item.Parent = this;
return List.Add(item);
}

public void Insert(int index, ChildClass item)
{
if (item != null) item.Parent = this;
List.Insert(ind ex, item);
}
}

Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 2 '07 #2
Great thanks Walter.
"Walter Wang [MSFT]" <wa****@online. microsoft.comwr ote in message
news:0W******** ******@TK2MSFTN GHUB02.phx.gbl. ..
Hi moondaddy,

The ChildClass will not have any reference to the ParentClass. Consider
this: an instance of ChildClass could be added to multiple ParentClass.

If you could guarantee an instance of ChildClass could only be added to
one
ParentClass. You could create a public property in the ChildClass to hold
the reference to ParentClass and let the ParentClass.Add or Insert to set
this reference.

class ChildClass
{
private ParentClass _parent;

internal ParentClass Parent
{
get { return _parent; }
set { _parent = value; }
}

}

class ParentClass : CollectionBase
{
public int Add(ChildClass item)
{
if (item != null) item.Parent = this;
return List.Add(item);
}

public void Insert(int index, ChildClass item)
{
if (item != null) item.Parent = this;
List.Insert(ind ex, item);
}
}

Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no
rights.

Mar 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3805
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I create a child object the constructor for the parent object is called. That works fine. But now I have the problem that I want to add an already existing Parent object to create a new Child object. How can this be done?
16
2667
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
9
5118
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In half-code this should in the end look like this: In the child class:
6
9416
by: Edwinah63 | last post by:
Hi everyone, could someone give me some thoughts on the best way to manage mdi parent and child forms? in vb6 i could scroll through the forms collection and determine which forms were open/closed and their current state. now i can't. before i didn't have to declare numerous named instances each time i loaded or unloaded a form.
10
4009
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
7
2030
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. How should I declare classes so that all three functions are executed when child class is called? Class Parent public function ONE 'code end fuction
10
19608
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public Class Parent '*** HOW TO DECLARE IT *** Dim Age As String = "1/1/2000"
2
1631
by: Bob | last post by:
I got three related datagrid views one parent and two children of the same. The two child tables contain many thousands of records and some of the contents are bitmap files in a sql server database. The default behaviour of loading all the contents of the parent data table and also all the related data is not acceptable, its takes too long to complete. What I need to do IMHO, is to load the parent table and after its loaded, and it gets...
2
2135
by: jeet232 | last post by:
I've a class 'Parent' I declare a map using map<Parent, Parent> mymap; Now I derive one child 'Child' and I've to add it to this map, so I do a static_cast like this: Child* key = new Child(); Child* val = new Child(); mymap.insert(pair<Parent, Parent>(static_cast<Parent&>(*key), static_cast<Parent&>(*val)));
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8388
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.