OK to assign an IDataObject reference to a DataObject variable

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

I do the following:

Note that I'm returning and interface because GetDatObject returns an
interface.

Public Shared Function GetContents() As DataObject

Dim ClipboardDataO As IDataObject = Clipboard.GetDataObject()

'assign an IDataObject reference to a DataObject variable

Return ClipboardDataO

End Function

End Class

elsewhere I do

Dim DataO As DataObject = GetContents()

That is I store the reference to the returned interface into an object of
class DataObject (not IDataObject)

It appears to work OK but now I noticed what I did and wonder why it is OK
to assign an IDataObject reference to a DataObject variable

Could you add some understanding the above?

Suppose I use DataO.GetHashCode (there is no IDataObject.GetHashCode) What
happens?

Thanks
 
What you are doing is entirely correct, and by design. It works because the
DataObject returned by Clipboard.GetDataObject() implements the IDataObject
interface.

Given your code, you might see fit to change

Public Shared Function GetContents() As DataObject

to

Public Shared Function GetContents() As IDataObject

and, subsequently

Dim DataO As DataObject = GetContents()

to

Dim DataO As IDataObject = GetContents()

since that is what you are creating internally, but it does depend on how
you go on to use the return value from this function.

HTH

Charles
 
Thanks for the insight


Charles Law said:
What you are doing is entirely correct, and by design. It works because
the DataObject returned by Clipboard.GetDataObject() implements the
IDataObject interface.

Given your code, you might see fit to change

Public Shared Function GetContents() As DataObject

to

Public Shared Function GetContents() As IDataObject

and, subsequently

Dim DataO As DataObject = GetContents()

to

Dim DataO As IDataObject = GetContents()

since that is what you are creating internally, but it does depend on how
you go on to use the return value from this function.

HTH

Charles
 
Back
Top