Enforce MustOverride for a derived Operation

G

Giovanni Bassi

Hello All,

I have a very simple problem.
I'd like a derived class to ALWAYS override the ToString Operation. For this
reason I have writen on the base class:

Public MustOverride Overloads Function ToString() As String

And on the Derived Class:

Public Overrides Function ToString() As String
'New overriden method
End Function

The problem is that if I use that line on the base class, it's derived
overriden form is never called. I end up getting the default result for a
ToString call to an object: the object's full name (like
"System.Data.SqlClient.SqlCommand").

Anybody knows why that is happening? Is there a way to do what I want?

Tks,

Giovanni Bassi
 
J

Jay B. Harlow [MVP - Outlook]

Giovanni Bassi said:
Hello All,

I have a very simple problem.
I'd like a derived class to ALWAYS override the ToString Operation. For this
reason I have writen on the base class:

Public MustOverride Overloads Function ToString() As String

And on the Derived Class:

Public Overrides Function ToString() As String
'New overriden method
End Function

The problem is that if I use that line on the base class, it's derived
overriden form is never called. I end up getting the default result for a
ToString call to an object: the object's full name (like
"System.Data.SqlClient.SqlCommand").

Anybody knows why that is happening? Is there a way to do what I want?

Tks,

Giovanni Bassi
 
J

Jay B. Harlow [MVP - Outlook]

Giovanni,
Unfortunately I do not see that VB.NET allows you to force an overridable
method "back" to mustoverride. I've tried before.
Public MustOverride Overloads Function ToString() As String

When you stated 'overloads' in your ToString definition, you effectively
created a new version of the ToString function that is no longer associated
with Object.ToString. (it has the newslot IL keyword on the IL function def,
you can use ILDASM to see the IL code produced) In other words you
Overloaded the ToString function without changing the parameters! This is
very similar to but not exactly like Shadowing.

In other words BaseClass.ToString is a different function than
Object.ToString. If you have an object variable and call ToString you will
get Object.ToString. If you have a BaseClass variable and call ToString you
will get Derived.ToString.

Dim base As BaseClass = New DerivedClass()
Dim obj As Object = base

base.ToString() ' will call Derived.ToString function
obj.ToString() ' will call Object.ToString

I would suggest you apply the Template Method Pattern here instead.

Public MustInherit Class BaseClass

Public Overrides Function ToString() As String
Return GetString()
End Function

Protected MustOverride Function GetString() As String

End Class

Where the GetString function all your derived classes must override, as the
"normal" overridden ToString in the base class will be calling it.

Hope this helps
Jay
 
T

Tom Spink

Hi Giovanni,

: Public MustOverride Overloads Function ToString() As String

Try changing that to:

Public MustOverride Shadows Function ToString() As String

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"


: Hello All,
:
: I have a very simple problem.
: I'd like a derived class to ALWAYS override the ToString Operation. For
this
: reason I have writen on the base class:
:
: Public MustOverride Overloads Function ToString() As String
:
: And on the Derived Class:
:
: Public Overrides Function ToString() As String
: 'New overriden method
: End Function
:
: The problem is that if I use that line on the base class, it's derived
: overriden form is never called. I end up getting the default result for a
: ToString call to an object: the object's full name (like
: "System.Data.SqlClient.SqlCommand").
:
: Anybody knows why that is happening? Is there a way to do what I want?
:
: Tks,
:
: Giovanni Bassi
:
:
 
J

Jay B. Harlow [MVP - Outlook]

Tom,
Try changing that to:
Public MustOverride Shadows Function ToString() As String
That's not going to help anything! As when he has a variable of type Object
the runtime will not call the MustOverride version of ToString.

Try a base class with a Derived class, with MustOveride and Shadows or
Overloads on the following code snippet.

Dim base As BaseClass = New DerivedClass()
Dim obj As Object = base

base.ToString() ' will call DerivedClass.ToString function
obj.ToString() ' will call Object.ToString

The base variable will call the Derived.ToString method. the obj variable
will call Object.ToString.

Public MustOverride BaseClass

' These two lines behave the same!
' Public MustOverride Overloads Function ToString() As String

Public MustOverride Shadows Function ToString() As String

End Class

Public Class DerivedClass : Inherits BaseClass

Public Overrides Function ToString() As String
Return "This is the DerivedClass!"
End Function

End Class

For a more complete explanation see my other post.

Hope this helps
Jay
 
G

Giovanni Bassi

Thanks Jay and Tom for the help. It is very much apreciated.
It will surely help.

Giovanni Bassi
 

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