When I try the following the results are unexpected. Any ideas will be most appreciated!
Class DataObjectSub
Inherits DataObject
End Class
Sub Test()
Dim myObject = New DataObjectSub
myObject.SetData("Test")
Clipboard.SetDataObject(myObject)
Dim myRetrievedObject As IDataObject = Clipboard.GetDataObject
Console.Write(myRetrievedObject.GetType.ToString)
Dim myRetrievedObject2=TryCast(Clipboard.GetDataObject,DataObjectSub)
End Sub
myRetrievedObject2 is Nothing
The output of the Write statement is: "System.Windows.Forms.DataObject". Shouldn't it be "DataObjectSub"? Am I missing something obvious?
Edited as requested:
In the case of Drag/Drop operations, that DataObject (implementing iDataObject) is again being used one can do this:
Sub TestDragDrop
Dim myObject as iDataObject=new DataObjectSub
someControl.DoDragDrop(myObject, myAllowedEffects)
End Sub
Later... in a DragOver event handler:
Sub anotherControl_DragOver(sender as object, e as DragEventArgs)
Dim myRetrievedObject1 as DataObjectSub = TryCast(e.Data, DataObjectSub)
Dim myRetrievedObject2 as IDataObject = e.Data
End Sub
works well and both myRetrievedObject1 and myRetrievedObject2 are not Nothing. The types of both retrieved objects are DataObjectSub.
Thanks for any ideas and patience! :)
This is not an unexpected behaviour.
Clipboard.GetDataObjectjust stores the data in theClipboard(as referred by MSDN) and its type isDataObject(as referred by your code). When you check its type, is irrelevant whether the input variable isDataObjector any other type (supported byGetDataObject). Bear in mind thatGetDataObjectis a method, whose returned value does not need to follow the default assignation rules (i.e., calling it does not provoke the same effects than assigning aStringvariable to anObjectvariable, for example).This code:
outputs exactly the same result than your code.
myRetrievedObject.GetType.ToStringjust checks the type ofGetDataObject, which is alwaysDataObject.UPDATE
After our discussion, I want to clarify that
Clipboard.GetDataObjectreturns aIDataObjectvariable (the interface ofDataObject, not a different type). Sample code to understand all this:As you can see (the two conditions are met, what means that all the variables are of type
DataObjectandIDataObjectat the same time), the relationshipDataObject/IDataObjectis not like the one between two different types. Actually, as shown bymyRetrievedObject3, the casting is a mere formal requirement (I am casting aDataObjectvariable toDataObject!).DataObject/IDataObjectare basically two sides of the same coin (which is calledDataObject). This implies that the condition below is true:That is, the type name for both
DataObjectandIDataObjectvariables isDataObject(or, more precisely:System.Windows.Forms.DataObject).