Holding Type Object

  • Thread starter Thread starter Zahid
  • Start date Start date
Z

Zahid

Hi,

I have a data item in a class that holds an Object
Variable. declared:

Public data As Object
'could be any one of 8 user defined objects

Each of the other objects had a method "toByteArray"
declared in them. I want to call the toByteArray method
on the object that the variable "data" holds - regardless
of which object it holds.

How do I do this? Intellisense only shows "GetType" as a
method call on the data variable.

Thanks in advance.
 
Cast it to the object in question and then call toByteArray. This is where
inheritance is very valuable. If all 8 classes derived from a common parent
class or intrerface, you could cast it to the parent and call the method.
 
Hi Chris,

All of the 8 objects (classes) only inherit from class
Object - ie they dont inherit from any user defined class.

Sorry, but how do you cast? Could you give a sample code
Please?

Thanks in advance.
 
Define an interaface that has a single method - ToByteArray.
Then derive each of your 8 classes from this interface and define the actual
ToByteArray implementation for each of the classes.
Now, when you need to use this method, simply cast an instance of the object
to this interface type and call ToByteArray on it
 
Back
Top