473,666 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OK to assign an IDataObject reference to a DataObject variable

Although the code is VB I believe the question is OOP
I tried the VB NG but no answer.
I suspect the c# programmer is more apt to be aware of these kinds of
subtleties so I', trying here.

I do the following:

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

Also note that DataObject implements IDataObject
====

Public Shared Function GetContents() As DataObject

Dim ClipboardDataO As IDataObject = Clipboard.GetDa taObject()

'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.GetHashCo de (there is no IDataObject.Get HashCode) What
happens?

Thanks


Dec 27 '05 #1
7 2111
The reason it works is because the call to GetDataObject on Clipboard
*happens* to return a DataObject, which implements IDataObject.

However, this is bad practice. If the method returns IDataObject, then
it could theoretically return ANY type that implements that interface, and
it would be right to do so.

So, in your case, don't work with DataObject, work with the interface
implementation instead.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

" **Developer**" <RE************ *@a-znet.com> wrote in message
news:ed******** ******@tk2msftn gp13.phx.gbl...
Although the code is VB I believe the question is OOP
I tried the VB NG but no answer.
I suspect the c# programmer is more apt to be aware of these kinds of
subtleties so I', trying here.

I do the following:

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

Also note that DataObject implements IDataObject
====

Public Shared Function GetContents() As DataObject

Dim ClipboardDataO As IDataObject = Clipboard.GetDa taObject()

'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.GetHashCo de (there is no IDataObject.Get HashCode) What
happens?

Thanks

Dec 27 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
The reason it works is because the call to GetDataObject on Clipboard
*happens* to return a DataObject, which implements IDataObject.

However, this is bad practice. If the method returns IDataObject, then
it could theoretically return ANY type that implements that interface, and
it would be right to do so.

So, in your case, don't work with DataObject, work with the interface
implementation instead.

Hope this helps.

If you turn Option Strict On, your compiler will tell you about it :-)

--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40 @*NO*SPAM*gmx.n et
Dec 27 '05 #3
Or, if you are using C#, the compiler will tell you about it as well. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Andreas Mueller" <me@privacy.net > wrote in message
news:41******** *****@individua l.net...
Nicholas Paldino [.NET/C# MVP] wrote:
The reason it works is because the call to GetDataObject on Clipboard
*happens* to return a DataObject, which implements IDataObject.

However, this is bad practice. If the method returns IDataObject,
then it could theoretically return ANY type that implements that
interface, and it would be right to do so.

So, in your case, don't work with DataObject, work with the interface
implementation instead.

Hope this helps.

If you turn Option Strict On, your compiler will tell you about it :-)

--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40 @*NO*SPAM*gmx.n et

Dec 27 '05 #4
It returns an IDataObject not a DataObject.
I say that because that is what I've read.
But what is an IDataObject?
An interface is basically a class without any code.
If an IDataObject is an instantiation of an interface, what is it?
Or is it an instantiation of a class that implements the interface?
Thanks

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:Oh******** ******@TK2MSFTN GP14.phx.gbl...
Or, if you are using C#, the compiler will tell you about it as well.
=)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Andreas Mueller" <me@privacy.net > wrote in message
news:41******** *****@individua l.net...
Nicholas Paldino [.NET/C# MVP] wrote:
The reason it works is because the call to GetDataObject on
Clipboard *happens* to return a DataObject, which implements
IDataObject.

However, this is bad practice. If the method returns IDataObject,
then it could theoretically return ANY type that implements that
interface, and it would be right to do so.

So, in your case, don't work with DataObject, work with the
interface implementation instead.

Hope this helps.

If you turn Option Strict On, your compiler will tell you about it :-)

--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40 @*NO*SPAM*gmx.n et


Dec 28 '05 #5
" **Developer**" <RE************ *@a-znet.com> wrote in
news:Og******** ******@TK2MSFTN GP14.phx.gbl:
Or is it an instantiation of a class that implements the interface?


Exactly! :)

-mdb
Dec 28 '05 #6
It's confusing (to me) because the doc says GetDataObject returns an
IDataObject.
The doc also says IDatObject is an interface.

Somehow, that doesn't sound the same as saying it is an object that
implements an interface.

Thanks


"Michael Bray" <mbray@makeDInt oDot_ctiusaDcom > wrote in message
news:Xn******** *************** *****@207.46.24 8.16...
" **Developer**" <RE************ *@a-znet.com> wrote in
news:Og******** ******@TK2MSFTN GP14.phx.gbl:
Or is it an instantiation of a class that implements the interface?


Exactly! :)

-mdb

Dec 29 '05 #7
" **Developer**" <RE************ *@a-znet.com> wrote in
news:uU******** ******@TK2MSFTN GP12.phx.gbl:
Somehow, that doesn't sound the same as saying it is an object that
implements an interface.


Keep in mind that any particular interface might be implemented by more
than one class. The point of what they are saying is that you should
NOT assume exactly which class it is that the function is returning. As
long as you only access the methods that are defined on the interface,
everything will be fine.

Its not so different from returning an object as one of the classes it
derives from... so for example,

public abstract class Vehicle { }
public class Car : Vehicle { }
public class Limo : Car { }
public class Bus : Vehicle { }

public class VehicleMaker
{
public static Vehicle MakeVehicle(int type)
{
if (type == 0) return new Car();
else if (type == 1) return new Limo();
else if (type == 2) return new Bus();
}
}

This is valid because Car, Limo, and Bus are all derived from Vehicle.
Now if you replace abstract class Vehicle with interface IVehicle, like
this:

public interface IVehicle { }
public class Car : IVehicle { }
public class Limo : Car { }
public class Bus : IVehicle { }

public class VehicleMaker
{
public static IVehicle MakeVehicle(int type)
{
if (type == 0) return new Car();
else if (type == 1) return new Limo();
else if (type == 2) return new Bus();
else throw new ArgumentExcepti on(...);
}
}

then nothing really has changed, except here the function is returning
an interface IVehicle instead of a class Vehicle. The result is the
same - you don't know (without checking types or whatnot) whether the
Vehicle/IVehicle that is returned is a Car, Bus, or Limo. But you know
for sure that any functions specified by the Vehicle abstract class or
the IVehicle interface will work on the object that MakeVehicle returns.

So really, when a function returns an "interface" , what it is saying is
that it is returning an object that implements that interface, but its
not going to tell you which one.

-mdb
Dec 29 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12567
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
0
386
by: DraguVaso | last post by:
Hi, I made some application (VB.NET 2005) based on classes in Windows Forms. I made a new Class Library Project and copied all of my classes and modules in it, to make a DLL that I could import afterwarths in Windows Forms applciations. The problem is that I get several errors indication that some methods aren't declared or defined when I try to build the project...
1
3556
by: Jinlin | last post by:
Does anyone know how to serialize a dataObject. Is it possible? What I try to do in my application is to allow user drag and drop objects onto a toolbox type of thing, and try to preserve these objects (DataObject) when user quit the application, so they could drop they back to an editor leter. I can successfully serialize the inner data of dataObject after GetData(formatType), but by converting to a specific format type, I lose some...
2
2186
by: **Developer** | last post by:
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
4
5962
by: Bob Staheli | last post by:
The .Net DataObject class implements the COM/OLE IDataObject interface , so how do I get it. I have tried this, but it does not work : // Declare the COM/OLE IDataObject interface public interface IOleDataObject {
3
7809
by: nicolas.hilaire | last post by:
Hi group, when using unmanaged class with my managed app, I've seen errors when including (for example) <windows.h>. One of theses erros is : IDataObject : ambiguous symbol error I've seen somewhere that to avoid this error, i've to remove all "using namespace XXXXX" from .h, and move it to .cpp.
42
1933
by: blisspikle | last post by:
I tried closely copying some code that I found on this group for assigning a type at runtime, but I cannot get it to work. Can someone see what is wrong with my logic? Thanks, Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As String s = "blah"
0
2782
by: =?ISO-8859-1?Q?=22Ro=DFert_G=2E_Schaffrath=22?= | last post by:
Sorry for the cross-posting. I am having a hard time trying to classify exactly what group this question would apply to. I had posted an earlier message to microsoft.public.dotnet.framework.windowsforms trying to find a way to access the lindex member of the FORMATETC structure for a CFSTR_FILECONTENTS callback request under C#. After some experimenting I was able to come up with the following code snippet that receives the callback...
3
1850
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello, I've got a recursive data model, where the root object gets a certain event handler: DataObject root = new DataObject(); root.DataObjectCreated += new DataObjectEventHandler(OnDataObjectCreated); Now the DataObject has a collection of sub data objects with following
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.