Inheritance question - I am new to this

  • Thread starter Thread starter Peter Hamilton
  • Start date Start date
P

Peter Hamilton

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
 
(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
([email protected])

" There's no place like 127.0.0.1 "

Please respond to the newsgroup,
so all can benefit.


One Day,
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
 
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.ParentName = "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



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
 
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.

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
 
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.
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
 
Back
Top