I am learning inheritance in VB.Net. A homework assignment asks me to write a base class with a name (String) property and a ToString method that displays the name. It also asks me to create some child classes of the base class and also implement a ToString method in those child classes. Is it possible for me to call the ToString method from both the child and base class?
Some example code is below:
My base class:
Public MustInherit Class MyBaseClass
Public Property Name as String
Public Overloads Function ToString() as String
Return Name
End Function
End Class
My child class:
Public Class ChildClass
Inherits MyBaseClass
Public Sub New()
Name = "This is the name"
End Sub
public Overloads Function ToString() as string
' Is it possible to call my base class ToString and append the text to
' this ToString method? I realize I can simply access my Name property
' however this does not fulfill the requirements i was given
return "Some text"
End Function
End Class
Some code that uses the above code:
dim someObject as new ChildClass("Hello VB.Net ")
lblLabel.text = someObject.ToString()
Again, I am wondering if there is a way to call the ToString() method within the child class and then base class to generate output something like: “Hello VB.Net This is the name”
1 Answer