Remedial course in OO; part II...

G

Gill Bates

Hope I'm not being a Pain In the A..

However; now I've got a similar issue for which I've seen no work
around...

Module Module1

Public Class BaseRow
Public Overridable Function Speak() As String
Return "BaseRow"
End Function
End Class

Public Class SpecializedRow
Inherits BaseRow
Public Overrides Function Speak() As String
Return "SpecializedRow"
End Function
End Class


Public Class Base1
Public Overridable Function NewRow() As BaseRow
Return New BaseRow
End Function
End Class


Public Class Derived1
Inherits Base1
Public Overrides Function NewRow() As SpecializedRow
Return New SpecializedRow
End Function
End Class


Public Sub main()
Dim b As New Base1
Dim d As New Derived1
Dim either As Base1

Console.WriteLine(b.NewRow.Speak)
Console.WriteLine(d.NewRow.Speak)

either = b
Console.WriteLine(either.NewRow.Speak)
either = d
Console.WriteLine(either.NewRow.Speak)
End Sub

End Module


The basic problem is:
Public Function NewRow() As SpecializedRow

in the derived class is being forced as Shadows.. I suppose I
understand why -- the signatures are the same; only diff is return
type (which is not part of a function sig); so Overload is not
approriate... however, the Overrides option is being totally rejected
by VB.Net (causes compile error) because:

Module1.vb(26): 'Public Overrides Function NewRow() As SpecializedRow'
cannot override 'Public Overridable Function NewRow() As BaseRow'
because they differ by their return types.

So, I'm not able to use Overloads or Overrides (also tried both
together) and is being forced to Shadows -- which is totally not what
I want. I want to be able to have a base class which will call the
approriate NewRow depending on the particular instance of the object
(Base or Derived). Maybe what I'm attempting is either:

1. not possible.
2. dumb.
3. or whatever...

Any suggestions?

Thanks
 
R

Rob Windsor [MVP]

Make the return type of Derived1.NewRow be BaseRow, that way the signature
will match the one in the base class and you'll be able to do the override.
Since SpecializedRow derives from BaseRow you'll still be able to create a
new instance of SpecializedRow and return it from the method (see below).
If you do this the code in Main() will work as you desire.

Public Class Derived1
Inherits Base1
Public Overrides Function NewRow() As BaseRow
Return New SpecializedRow
End Function
End Class
 
G

Gill Bates

Smack. "sound of hand hitting forehead"...

Roger that...

Thanks...


Rob Windsor said:
Make the return type of Derived1.NewRow be BaseRow, that way the signature
will match the one in the base class and you'll be able to do the override.
Since SpecializedRow derives from BaseRow you'll still be able to create a
new instance of SpecializedRow and return it from the method (see below).
If you do this the code in Main() will work as you desire.

Public Class Derived1
Inherits Base1
Public Overrides Function NewRow() As BaseRow
Return New SpecializedRow
End Function
End Class

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


Gill Bates said:
Hope I'm not being a Pain In the A..

However; now I've got a similar issue for which I've seen no work
around...

Module Module1

Public Class BaseRow
Public Overridable Function Speak() As String
Return "BaseRow"
End Function
End Class

Public Class SpecializedRow
Inherits BaseRow
Public Overrides Function Speak() As String
Return "SpecializedRow"
End Function
End Class


Public Class Base1
Public Overridable Function NewRow() As BaseRow
Return New BaseRow
End Function
End Class


Public Class Derived1
Inherits Base1
Public Overrides Function NewRow() As SpecializedRow
Return New SpecializedRow
End Function
End Class


Public Sub main()
Dim b As New Base1
Dim d As New Derived1
Dim either As Base1

Console.WriteLine(b.NewRow.Speak)
Console.WriteLine(d.NewRow.Speak)

either = b
Console.WriteLine(either.NewRow.Speak)
either = d
Console.WriteLine(either.NewRow.Speak)
End Sub

End Module


The basic problem is:
Public Function NewRow() As SpecializedRow

in the derived class is being forced as Shadows.. I suppose I
understand why -- the signatures are the same; only diff is return
type (which is not part of a function sig); so Overload is not
approriate... however, the Overrides option is being totally rejected
by VB.Net (causes compile error) because:

Module1.vb(26): 'Public Overrides Function NewRow() As SpecializedRow'
cannot override 'Public Overridable Function NewRow() As BaseRow'
because they differ by their return types.

So, I'm not able to use Overloads or Overrides (also tried both
together) and is being forced to Shadows -- which is totally not what
I want. I want to be able to have a base class which will call the
approriate NewRow depending on the particular instance of the object
(Base or Derived). Maybe what I'm attempting is either:

1. not possible.
2. dumb.
3. or whatever...

Any suggestions?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top