473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create 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 "inheritance" 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

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

MessageBox.Show("oParent.ParentName = '" & oParent.ParentName & "'" & vbCrLf & vbCrLf & "oChild.ParentName = '" & oChild.ParentName & "'") '//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 1624
(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**********@ntlworld.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**************@TK2MSFTNGP12.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 "inheritance" 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim oParent As New Parent() '//Parent Object
Dim oChild As New Child() '//Child inherits from Parent
oParent.ParentName = "I set this for the parent!" '//Set the parent's value
MessageBox.Show("oParent.ParentName = '" & oParent.ParentName & "'" &
vbCrLf & vbCrLf & "oChild.ParentName = '" & oChild.ParentName & "'") '//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.ParentNam e = "I set this for the parent!" '//Set the parent's value
msgbox ( "a is " & a.tostring & " b is " & b.tostring) ***************MessageBox.Show("oParent.ParentName = '" & oParent.ParentName & "'" & vbCrLf & vbCrLf & "oChild.ParentName = '" & oChild.ParentName & "'")
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 CheckPersonality(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.child" then 'There is probably a better way to check the type but I don't know it.
if ctype(oParent,child).tantrum > 5 then 'by using CTYPE you have access to the child properties
return oParent.FamilyName & " is prone to tantrums"
else
return oParent.FamilyName & " is not prone to tantrums"
end if
else ' really is a parent
if CheckForHissyFits(oParent)
return oParent.FamilyName & " is a Parent and prone to Hissy Fits"
else
return oParent.FamilyName & " 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**************@TK2MSFTNGP12.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 "inheritance" 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

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

MessageBox.Show("oParent.ParentName = '" & oParent.ParentName & "'" & vbCrLf & vbCrLf & "oChild.ParentName = '" & oChild.ParentName & "'") '//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**************@TK2MSFTNGP12.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 "inheritance" 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

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

MessageBox.Show("oParent.ParentName = '" & oParent.ParentName & "'" & vbCrLf & vbCrLf & "oChild.ParentName = '" & oChild.ParentName & "'") '//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**************@TK2MSFTNGP12.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 "inheritance" 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

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

MessageBox.Show("oParent.ParentName = '" & oParent.ParentName & "'" & vbCrLf & vbCrLf & "oChild.ParentName = '" & oChild.ParentName & "'") '//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
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...
2
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...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
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...
5
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...
22
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...
60
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...
7
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...
6
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...
6
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,...
0
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,...
0
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,...
1
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...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.