473,796 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance question - I am new to this

I am trying to implement inheritance but I am having a difficult time with some concepts.

What I am trying to do is have a Child object inherit from the Parent, and when you set a property value at the Parent level, I want all of the children to see this change. Likewise, when I set a Parent's property value in the child object, both the parent and all of the children see the change.

Maybe what I am trying to do is not "inheritanc e" after all. But, if anyone can point me in the right direction it would be appreciated.

Sample simple VB code is below (I have omitted the #Region " Windows Form Designer generated code " stuff)
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

Dim oParent As New Parent() '//Parent Object
Dim oChild As New Child() '//Child inherits from Parent
oParent.ParentN ame = "I set this for the parent!" '//Set the parent's value

MessageBox.Show ("oParent.Paren tName = '" & oParent.ParentN ame & "'" & vbCrLf & vbCrLf & "oChild.ParentN ame = '" & oChild.ParentNa me & "'") '//This returns an empty string!! (I want the child to know about its parent's value!!!)

End Sub

End Class

Public Class Parent

Private m_ParentName As String
Public Property ParentName()

Get

Return m_ParentName

End Get

Set(ByVal Value)

m_ParentName = Value

End Set

End Property

End Class

'//No methods, I just want to see if we can see the parent's name...

Public Class Child

Inherits Parent

End Class
Jul 19 '05 #1
4 1645
(Please don't post in HTML)

Hi, you can't do what you are trying to do about a parent setting it's
childrens value. How does a parent know which class' are being derived.

However, you can access the methods/properties of the class you are
inheriting from by using the MyBase statement.

--
Happy to help,
-- Tom Spink
(th**********@n tlworld.com)

" There's no place like 127.0.0.1 "

Please respond to the newsgroup,
so all can benefit.
One Day,
"Peter Hamilton" <ph***********@ hotmail.com> wrote in message
news:uP******** ******@TK2MSFTN GP12.phx.gbl...
I am trying to implement inheritance but I am having a difficult time with
some concepts.

What I am trying to do is have a Child object inherit from the Parent, and
when you set a property value at the Parent level, I want all of the
children to see this change. Likewise, when I set a Parent's property value
in the child object, both the parent and all of the children see the change.

Maybe what I am trying to do is not "inheritanc e" after all. But, if anyone
can point me in the right direction it would be appreciated.

Sample simple VB code is below (I have omitted the #Region " Windows Form
Designer generated code " stuff)
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim oParent As New Parent() '//Parent Object
Dim oChild As New Child() '//Child inherits from Parent
oParent.ParentN ame = "I set this for the parent!" '//Set the parent's value
MessageBox.Show ("oParent.Paren tName = '" & oParent.ParentN ame & "'" &
vbCrLf & vbCrLf & "oChild.ParentN ame = '" & oChild.ParentNa me & "'") '//This
returns an empty string!! (I want the child to know about its parent's
value!!!)
End Sub
End Class
Public Class Parent
Private m_ParentName As String
Public Property ParentName()
Get
Return m_ParentName
End Get
Set(ByVal Value)
m_ParentName = Value
End Set
End Property
End Class
'//No methods, I just want to see if we can see the parent's name...
Public Class Child
Inherits Parent
End Class
Jul 19 '05 #2
Hi Peter,
Your problem is that you have two different instances of objects. ie oParent and oChild
Setting a property on one does not set the same property on the other. Inheritance allows you derive special types of the base class. ie A child is a parent with special properties, methods etc.
What you have done at present is not different to:
Dim a as int32********** *********Dim oParent As New Parent() '//Parent Object
Dim b as int64 *************** ** Dim oChild As New Child() '//Child inherits from Parent
a=1************ *************** ***oParent.Pare ntName = "I set this for the parent!" '//Set the parent's value
msgbox ( "a is " & a.tostring & " b is " & b.tostring) *************** MessageBox.Show ("oParent.Paren tName = '" & oParent.ParentN ame & "'" & vbCrLf & vbCrLf & "oChild.ParentN ame = '" & oChild.ParentNa me & "'")
b has never been set.

The thrust of what you are trying to do if I understand is:
Base class (Parent in your case) has a common property say Familyname and a special property (HissyFits) :)
derived Class (Child in your case) has some special properties that parent doesn't have. eg (Tantrum) :)
So:
The Familyname and hissyfits property should only be defined in the base class.
The tantrum property should only be defined in the derived class
Now when you instantiate a child you can get access to the Familyname, hissyfits, and tantrum properties. (You can stop Child access to Hissyfits but it is a bit more complicated).
When you instantiate a parent you can only get access to the Familyname and the hissyfits.
You can pass a child object to any chunk of code that is expecting a parent and still get to its child properties by using 'CTYPE'

eg Public CheckPersonalit y(by person as parent) as string
dim oParent as parent
oParent=person 'this assignment is not really necessary you could work straight off the 'person' The point is that oParent takes on the class and data of the passed in object.
if oParent.gettype .tostring = "myproject.chil d" then 'There is probably a better way to check the type but I don't know it.
if ctype(oParent,c hild).tantrum > 5 then 'by using CTYPE you have access to the child properties
return oParent.FamilyN ame & " is prone to tantrums"
else
return oParent.FamilyN ame & " is not prone to tantrums"
end if
else ' really is a parent
if CheckForHissyFi ts(oParent)
return oParent.FamilyN ame & " is a Parent and prone to Hissy Fits"
else
return oParent.FamilyN ame & " is a Parent and not prone to Hissy Fits"
end if
end if
If you hardcoded the FamilyName property to "Smith" then all child class objects would be 'Smiths' but I am guessing that what you want to do is declare a child, set the familyname property in a routine that
is expecting a parent and later on deal with the object as a child again.
HTH
Bob

"Peter Hamilton" <ph***********@ hotmail.com> wrote in message news:uP******** ******@TK2MSFTN GP12.phx.gbl...
I am trying to implement inheritance but I am having a difficult time with some concepts.

What I am trying to do is have a Child object inherit from the Parent, and when you set a property value at the Parent level, I want all of the children to see this change. Likewise, when I set a Parent's property value in the child object, both the parent and all of the children see the change.

Maybe what I am trying to do is not "inheritanc e" after all. But, if anyone can point me in the right direction it would be appreciated.

Sample simple VB code is below (I have omitted the #Region " Windows Form Designer generated code " stuff)
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

Dim oParent As New Parent() '//Parent Object
Dim oChild As New Child() '//Child inherits from Parent
oParent.ParentN ame = "I set this for the parent!" '//Set the parent's value

MessageBox.Show ("oParent.Paren tName = '" & oParent.ParentN ame & "'" & vbCrLf & vbCrLf & "oChild.ParentN ame = '" & oChild.ParentNa me & "'") '//This returns an empty string!! (I want the child to know about its parent's value!!!)

End Sub

End Class

Public Class Parent

Private m_ParentName As String
Public Property ParentName()

Get

Return m_ParentName

End Get

Set(ByVal Value)

m_ParentName = Value

End Set

End Property

End Class

'//No methods, I just want to see if we can see the parent's name...

Public Class Child

Inherits Parent

End Class
Jul 19 '05 #3
It is not possible to unambiguously provide you a solution without knowing what the Parent and Child represent in reality. But analysing the names you have used we can say " Parent 'has' children" *BUT* the code you have written would translate to something like "A child 'is a type' of Parent" which to me doesn't sound alright. The simple rule of thumb for inheritance is :

Use it for 'Is a type of' relationships only. And use containment for a 'has' relationship. For the latter you will need to establish an object model to show the containment hierarchy.

"Peter Hamilton" <ph***********@ hotmail.com> wrote in message news:uP******** ******@TK2MSFTN GP12.phx.gbl...
I am trying to implement inheritance but I am having a difficult time with some concepts.

What I am trying to do is have a Child object inherit from the Parent, and when you set a property value at the Parent level, I want all of the children to see this change. Likewise, when I set a Parent's property value in the child object, both the parent and all of the children see the change.

Maybe what I am trying to do is not "inheritanc e" after all. But, if anyone can point me in the right direction it would be appreciated.

Sample simple VB code is below (I have omitted the #Region " Windows Form Designer generated code " stuff)
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

Dim oParent As New Parent() '//Parent Object
Dim oChild As New Child() '//Child inherits from Parent
oParent.ParentN ame = "I set this for the parent!" '//Set the parent's value

MessageBox.Show ("oParent.Paren tName = '" & oParent.ParentN ame & "'" & vbCrLf & vbCrLf & "oChild.ParentN ame = '" & oChild.ParentNa me & "'") '//This returns an empty string!! (I want the child to know about its parent's value!!!)

End Sub

End Class

Public Class Parent

Private m_ParentName As String
Public Property ParentName()

Get

Return m_ParentName

End Get

Set(ByVal Value)

m_ParentName = Value

End Set

End Property

End Class

'//No methods, I just want to see if we can see the parent's name...

Public Class Child

Inherits Parent

End Class
Jul 19 '05 #4
Ben
Remember from a design point if B derives from A, you should alwatys be able to say B "is a" A (but in a more specialized form) otherwise perphaps inheritance is not the way.
"Peter Hamilton" <ph***********@ hotmail.com> wrote in message news:uP******** ******@TK2MSFTN GP12.phx.gbl...
I am trying to implement inheritance but I am having a difficult time with some concepts.

What I am trying to do is have a Child object inherit from the Parent, and when you set a property value at the Parent level, I want all of the children to see this change. Likewise, when I set a Parent's property value in the child object, both the parent and all of the children see the change.

Maybe what I am trying to do is not "inheritanc e" after all. But, if anyone can point me in the right direction it would be appreciated.

Sample simple VB code is below (I have omitted the #Region " Windows Form Designer generated code " stuff)
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

Dim oParent As New Parent() '//Parent Object
Dim oChild As New Child() '//Child inherits from Parent
oParent.ParentN ame = "I set this for the parent!" '//Set the parent's value

MessageBox.Show ("oParent.Paren tName = '" & oParent.ParentN ame & "'" & vbCrLf & vbCrLf & "oChild.ParentN ame = '" & oChild.ParentNa me & "'") '//This returns an empty string!! (I want the child to know about its parent's value!!!)

End Sub

End Class

Public Class Parent

Private m_ParentName As String
Public Property ParentName()

Get

Return m_ParentName

End Get

Set(ByVal Value)

m_ParentName = Value

End Set

End Property

End Class

'//No methods, I just want to see if we can see the parent's name...

Public Class Child

Inherits Parent

End Class
Jul 19 '05 #5

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

Similar topics

0
1464
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing works fine in that the derived classes can use the base method get, but if I try and add a method only available to Derived2, eg, get2, I get a compiler error on the call to add_varargs_method("get2",&Derived2::get2, "get2()\n"); in...
2
4382
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
2
4339
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
2901
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
5
2183
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
22
23386
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
60
4946
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
7
3741
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
6
3828
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused me somewhat as I have always thought you get code reuse "for free" with inheritance. Am I missing something?. Will someone care to explain ??
6
4412
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans, Karthik Balaguru
0
9673
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
10452
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
10221
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...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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
7546
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
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
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...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.