Jay B. Harlow said:
Ot,
It avoids your routine because you Shadowed Enqueue & Dequeue, the Shadowed
version are only available when you have a variable of type IntQueue, when
you have a variable of type Queue, the compiler only sees the Queue
versions.
I am getting what I want. That is, objects of type IntQueue are
type-specific queues. (Well, I have to shadow Contains as well if I want
to make that one type-specific, but the idea is there.)
Further discussion is simply for the sake of learning more.
Actually saying Dim aQ as Queue = New IntQueue would be a programming error
in my mind.
I tried this and when I changed the Shadows keyword to Overrides the IDE
tell me
<qoute>
sub 'Enqueue' cannot be declared 'Overrides' because it does not override
a sub in a base class.
</quote>
and also
<quote>
sub 'Enqueue' shadows an overloadable member declared in the base class
'Queue'. If you want to overload the base method, this method must be
declared 'Overloads'.
</quote>
I am not quite sure what the first error message is telling me. It seems
that "Enqueue" is a sub in the base class, but the error message says it
isn't. Confused as always. There seems no way to use Overrides.
The second error message (when I use the Overrides not Shadows) says it
shadows? More confusion. Well, anyway, I tried overloading and it
behaves almost the way I want, too:
Option Strict On
Public Class IntQueue
Inherits Queue
Public Overloads Sub Enqueue(ByVal obj As Integer)
Console.WriteLine("IntQueue.Enqueue entered")
MyBase.Enqueue(obj)
End Sub
Public Overloads Sub Enqueue(ByVal obj As Object)
Throw New SystemException("IntQueue requires Integer parameter")
End Sub
Public Shadows Function Dequeue() As Integer
Console.WriteLine("IntQueue.Dequeue entered")
Return CInt(MyBase.Dequeue())
End Function
End Class
The disadvantage to this class is that the error only occurs at runtime.
The IDE doesn't ensure that the passed parameter is only integer.
Generally I will use encapsulation to create an IntQueue object, instead of
Inheritance, as you cannot change the types of the overriden types. Rarely
do you want to Shadow a method, as the result is rarely what you want. The
only place I (currently) use Shadows normally is when I have creating
derived controls and I need to change one of the Attributes on the
properties. I shadow the base property so I can add the attribute, however
the shadowed property does not attempt to change types, it simply calls the
base function. Overall the rule I use for Shadows is its use is the
exception not the norm!
Yes, that works as well. By using the Shadows I don't have to Shadow non
type-specific methods (like clear()). If I do it this way I have to mirror
all of the non type-specific methods as well so my class functions as a
full-blown queue.
Remember CInt & CType are conversion operators, while DirectCast is casting.
(CType will cast in some cases and convert in most cases, hence I use
DirectCast unless I have to use CType).
I apparently have a bit to learn about Casting and Conversion. I have been
thinking of them as the same which your statement implies is wrong
thinking.
I found this in the VB Language reference:
<quote>
The DirectCast keyword introduces a type conversion operation. You use it
the same way you use the CType keyword....
Both keywords take an expression to be converted as the first argument, and
the type to convert it to as the second argument. Both conversions fail if
there is no conversion defined between the data type of the expression and
the data type specified as the second argument.
The difference between the two keywords is that CType succeeds as long as
there is a valid conversion defined between the expression and the type,
whereas DirectCast requires the run-time type of an object variable to be
the same as the specified type. If the specified type and the run-time type
of the expression are the same, however, the run-time performance of
DirectCast is better than that of CType.
DirectCast is special in that conversions from type Object to any other
type are performed as a direct cast down the hierarchy — all of the special
conversion behaviors from Object are ignored.
</quote>
This last sentence is a bit unclear to me. I am not sure what "special
conversion behaviors" might be in an Object. Perhaps you can help?
I appreciate your taking the time to discuss this with me, Jay. I hope the
lurkers are learning as fast as I am.
Regards,
Ot