Hi Bob,
Pardon me if this is irrelevant Bob, I'm not sure I understand the question properly so a bit of a ramble about Interfaces...
|| Sub SomeSub(ByVal Dictionary as IDictionary)
You can't just call this with any old variable - it has to be one whose class implements the given interface. In this case, for
example, a HashTable.
One of the purposes of using an interface for a method parameter is to restrict what can be done with the variable. For example,
the HashTable provides Clone, ContainsKey and ContainsValue, amongst others. These are not available to the Dictionary parameter as
they are HashTable specific.
It's perhaps clearer if you have two disparate objects, say an Elephant and a BananaBalloon (as at the seaside). Both of these
might implement an IPeopleCarrier interface with such properties as Capacity, etc.
There's no way that you could define a method which takes either an Elephant or a BananaBalloon but no other object - except by
using the interface, eg Sub LoadUp (oCarrier As IPeopleCarrier).
=================================
Having written this, and played around in VB, I wasn't happy that I was answering you, so I decided to try the same in C#.
|| GetType isn't availble for variables decalred as interface types
True, I'm afraid. Have a look at the following C# code:
static void Main()
{ HmmmInterfaces (new Hashtable()); }
static void HmmmInterfaces (IDictionary d)
{
Hashtable h = new Hashtable();
Type th = h.GetType();
Type td = d.GetType(); // Won't compile in VB. It says that
// GetType is not a member of IDictionary.
}
This is perfectly acceptable C# and, with this code, td is HashTable.
In VB you can't get call GetType on d.
So now I think I understand your question. - How are you supposed to know what you are dealing with?
Well, you're right about the DirectCast.
This will give HashTable
Dim td As Type = DirectCast(d, Object).GetType
If td Is GetType(Hashtable) Then
And TypeOf is available too.
This will be True only for HashTable
If TypeOf d Is Hashtable Then
This will be always be True (as the parameter is an IDictionary)
If TypeOf d Is IDictionary Then
|| Does this mean that it is possible to have variable types that
|| are not derived from Object?
No, I just think that while GetType is available in C#, it has, for some arbitrary (ie I can't fathom it
) reason, has been
removed from Interfaces in VB.
I hope this is of some use to you.
Regards,
Fergus