Resolved Interface vs. Object

aaaron

Well-known member
Joined
Jan 23, 2011
Messages
219
Programming Experience
10+
My understanding is that an Interface does not have memory allocated like a class's object does.

The clipboard rerurns an IDataObject which I assume is an object not an interface even thought it's name starts with an I.

Correct?

Consider the following:
Dim dict As IDictionary = CType(Activator.CreateInstance(tGeneric), IDictionary)

It appears an object is being created and typed an interface.
Data is then added to the interface, but I thought an interface was simply a recipe with no memory allocated.

If it's actually an object not simplty a recipy, how does the above differ from:
Dim dict = New Dictionary...?

Help!!
 
Last edited:
Solution
Looking at the source the managed Clipboard class interacts with OLE clipboard with a COM interface, the returned object is DataObject if the returned data is a COM object, otherwise it is some kind of OLE object that can be interfaced through IDataObject. Also looking at source for DataObject it looks like it is safe to CType anyway since it can convert through an internal OleConverter.

IDataObject has only a few basic members for interfacing the object returned from clipboard (SetData, GetData, GetDataPresent, GetDataFormats), while DataObject has a richer menu with members like Get/Set Text, Image, Audio and FileDropList. All these you can get/set with SetData/GetData also directly on Clipboard class or through the IDataObject...
Clipboard.GetDataObject return a DataObject, which implements interface IDataObject. For opposite SetDataObject parameter data must be "an object that implements IDataObject".

CreateInstance "creates an instance of the specified type using the constructor that best matches". It is no different from creating object with New operator.

Classes implement interfaces. Objects are instances of classes. You can "view" (DirectCast/Ctype) the object as interface type and "see" only the interface members, including calling the methods. Different classes can implement the same interface and handled as same type, for example put in a collection of the interface type.
 
Programming interfaces work much as physical interfaces do in the real world. Think about USB. USB is an interface. The USB specification describes what a USB device has and what a USB device does. That interface is implemented by all manner of different types of objects. Printer is a class of object. External hard drive is a class of object. Etc. Each object of any of those types implements the USB interface. They each have various other functionality specific to their class but they each implement the functionality defined by the USB interface specification. In programming, USB would be an interface, printer and external hard drive would be classes and each printer and external hard drive you use would be objects. The whole point of OOP is that it mimics real-world objects and how they behave.
 
Clipboard.GetDataObject return a DataObject, which implements interface IDataObject. For opposite SetDataObject parameter data must be "an object that implements IDataObject".

Please comment on the following:

Untitled.png


Does this relate to your statment about viewing?
 
Last edited:
Use CType/DirectCast for explicit cast as intellisense fixes suggest:
1685788438924.png
 
It saying it returns an IDataObject not a DataObject - right?

And the IDataObject is actualy an Obect not an interface - right?

This has been the crux of by confusion for years.
 
Last edited:
Looking at the source the managed Clipboard class interacts with OLE clipboard with a COM interface, the returned object is DataObject if the returned data is a COM object, otherwise it is some kind of OLE object that can be interfaced through IDataObject. Also looking at source for DataObject it looks like it is safe to CType anyway since it can convert through an internal OleConverter.

IDataObject has only a few basic members for interfacing the object returned from clipboard (SetData, GetData, GetDataPresent, GetDataFormats), while DataObject has a richer menu with members like Get/Set Text, Image, Audio and FileDropList. All these you can get/set with SetData/GetData also directly on Clipboard class or through the IDataObject interface, for example (Clipboard. or IDataObject.) GetData(DataFormats.Bitmap) that returns Object that you then can cast as Bitmap.
 
Solution
Every once in a while there is an answer-post that is extraordinary. I assume everyone would put this in that class.

Too bad there wasn't a way to sort answers to see that kind of posts. Would make great reading.
 
Back
Top