473,782 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data.GetData(.. .) when exact type is not known?

I'm making a drag and drop application, and in the *_DragDrop(...)-method I
have the following code:

private void SelectTree_Drag Drop(object sender,
System.Windows. Forms.DragEvent Args e)
{
Object item = (object)e.Data. GetData(typeof( AbstractNode));
// Other code
}

My problem is that the type inside the brackets of typeof(...) is not
AbstractNode, instead the type can be a varity of types which all are
inherited from AbstractNode, i e I have a lot of node classes that all
inherits from the class AbstractNode.

How do I handle this problem? Of course, I don't want to handle every type
with some if statement.

Nov 16 '05 #1
6 7145
Andy,

The drag and drop operation should use serialization, so you should be
able to get the specific type by calling the GetType method on the item
parameter.

However, you should really cast this to a base class, or an interface
implmentation which has the contract defined for the functionality that you
wish to execute. This way, you don't have to make specific casts to
specific types.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Andy" <do**@spam.me > wrote in message
news:eO******** ******@tk2msftn gp13.phx.gbl...
I'm making a drag and drop application, and in the *_DragDrop(...)-method
I
have the following code:

private void SelectTree_Drag Drop(object sender,
System.Windows. Forms.DragEvent Args e)
{
Object item = (object)e.Data. GetData(typeof( AbstractNode));
// Other code
}

My problem is that the type inside the brackets of typeof(...) is not
AbstractNode, instead the type can be a varity of types which all are
inherited from AbstractNode, i e I have a lot of node classes that all
inherits from the class AbstractNode.

How do I handle this problem? Of course, I don't want to handle every type
with some if statement.

Nov 16 '05 #2
> The drag and drop operation should use serialization, so you should be
able to get the specific type by calling the GetType method on the item
parameter.
Sorry, I don't really understand what you mean. If I don't know the type,
how do I use the GetData(...)?
Would you please give a short example?
However, you should really cast this to a base class, or an interface
implmentation which has the contract defined for the functionality that you wish to execute. This way, you don't have to make specific casts to
specific types.


This I understand!

Nov 16 '05 #3
Andy,

I think I see what you mean now. Basically, the clipboard places the
specific type into the clipboard, and you don't know what that type is on
the way out.

To get around this, I would create a structure with a reference to your
type, like this:

[Serializable]
public struct AbstractNodeWra pper
{
public AbstractNode Node;
}

Then, instead of placing an instance of your abstract node on the
clipboard, create an instance of this, set the Node field to the node, and
then pass that to the DoDragDrop method.

Then, in your handler, get this structure from the GetData method, and
then call the methods you need to on the Node property.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Andy" <do**@spam.me > wrote in message
news:OR******** *****@TK2MSFTNG P10.phx.gbl...
The drag and drop operation should use serialization, so you should
be
able to get the specific type by calling the GetType method on the item
parameter.


Sorry, I don't really understand what you mean. If I don't know the type,
how do I use the GetData(...)?
Would you please give a short example?
However, you should really cast this to a base class, or an interface
implmentation which has the contract defined for the functionality that

you
wish to execute. This way, you don't have to make specific casts to
specific types.


This I understand!

Nov 16 '05 #4
> [Serializable]
public struct AbstractNodeWra pper
{
public AbstractNode Node;
}

Then, instead of placing an instance of your abstract node on the
clipboard, create an instance of this, set the Node field to the node, and
then pass that to the DoDragDrop method.

Then, in your handler, get this structure from the GetData method, and
then call the methods you need to on the Node property.


Well, this would work I think, though it's a little bit ugly.

Though I find it a little hard to accept that it doesn't work directly. I
mean, say that the data is of type MyNode which is inherited from
AbstractNode, then data also should be of type AbstractNode.

I tried this, which works, but it's too ugly:

private void SelectTree_Drag Drop(object sender,
System.Windows. Forms.DragEvent Args e)
{
string type = e.Data.GetForma ts().GetValue(1 ).ToString();
AbstractNode node = (AbstractNode) e.Data.GetData( type);

// Other code
}
Nov 16 '05 #5
Andy,

The problem with the way you have it is that if you place more than one
type of format on the clipboard, then you might not be guaranteed that you
will be getting that format back when you get the data from the drag drop
operation. The clipboard is designed in such a way that you can get
multiple formats for the same data, so that different applications can
handle it. For example, if you place XHTML on the clipboard, it will
indicate that there is XML, HTML, and text on the clipboard, and the
application can look for a specific type and use whichever best suits its
needs.

Now, when you place the type directly on the clipboard, it is
serializing the type, and the format that it is placing on the clipboard is
the name of the type, not of the root type that it derives from. This is
why using typeof(Abstract Node) when trying to retrieve the data fails.

When you use the structure, the data is serialized to the field, and the
type that is placed on the clipboard is consistent. It's cleaner than the
way that you use, since you are guaranteed a type of that format (and aren't
just taking a stab at the first format that is on the clipboard).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Andy" <do**@spam.me > wrote in message
news:eY******** ******@TK2MSFTN GP15.phx.gbl...
[Serializable]
public struct AbstractNodeWra pper
{
public AbstractNode Node;
}

Then, instead of placing an instance of your abstract node on the
clipboard, create an instance of this, set the Node field to the node,
and
then pass that to the DoDragDrop method.

Then, in your handler, get this structure from the GetData method,
and
then call the methods you need to on the Node property.


Well, this would work I think, though it's a little bit ugly.

Though I find it a little hard to accept that it doesn't work directly. I
mean, say that the data is of type MyNode which is inherited from
AbstractNode, then data also should be of type AbstractNode.

I tried this, which works, but it's too ugly:

private void SelectTree_Drag Drop(object sender,
System.Windows. Forms.DragEvent Args e)
{
string type = e.Data.GetForma ts().GetValue(1 ).ToString();
AbstractNode node = (AbstractNode) e.Data.GetData( type);

// Other code
}

Nov 16 '05 #6
Thanks for the lesson! :-)

I think I understand fully now!

The problem with the way you have it is that if you place more than one type of format on the clipboard, then you might not be guaranteed that you
will be getting that format back when you get the data from the drag drop
operation. The clipboard is designed in such a way that you can get
multiple formats for the same data, so that different applications can
handle it. For example, if you place XHTML on the clipboard, it will
indicate that there is XML, HTML, and text on the clipboard, and the
application can look for a specific type and use whichever best suits its
needs.

Now, when you place the type directly on the clipboard, it is
serializing the type, and the format that it is placing on the clipboard is the name of the type, not of the root type that it derives from. This is
why using typeof(Abstract Node) when trying to retrieve the data fails.

When you use the structure, the data is serialized to the field, and the type that is placed on the clipboard is consistent. It's cleaner than the
way that you use, since you are guaranteed a type of that format (and aren't just taking a stab at the first format that is on the clipboard).

Nov 16 '05 #7

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

Similar topics

3
6189
by: Roberto | last post by:
I have the following problem: I have the following form client side: <FORM.......> <FORM action="./WZUpload.asp" method="Post" enctype="multipart/form-data" WIDTH=100%> <INPUT Type="file" name="UpFileName" STYLE="{Width:400px}"><BR>
6
1970
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter.and no of parameter is calling function can be anything. for example.suppose my function is function2. then when i call function1(int i ,char j,float d) { function2()
13
10448
by: dawatson833 | last post by:
I have several stored procedures with parameters that are defined with user defined data types. The time it takes to run the procedures can take 10 - 50 seconds depending on the procedure. If I change the parameter data types to the actual data type such as varchar(10), etc., the stored procedure takes less that a second to return records. The user defined types are mostly varchar, but some others such as int. They are all input type...
4
22137
by: Robert W. | last post by:
I have a data model that has dozens of properties of most every data type. I now want to declare a few of these properties as Images but can't seem to do it. The approach I thought would work was this one: private Image _picture; public Image Picture { get
13
4535
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only known at run time. Therefore a lot of template based trick isn't too useful. Considering datafile float x(3) 3.5, 2.5, 8.9 double y(3) 2.7, -2.3, 1.2 int z(3) 5, 2, 3
4
2411
by: donald | last post by:
how to get the length of variable for differernt data type, such as data type of structure, integer, date, etc. Thanks in advance.
7
2862
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please explain me what does the above statement mean? <script runat="server"> Class Clock
4
35490
by: bleighfield | last post by:
Hi everyone Hope someone can help with this one.. Background: I work in vehicle fleet, I have built something to 'predict' when a car/van service is due (it's fairly simple, calculates miles per day between 2 known dates/mileages and then uses the service schedule to work out what date the next service would be due, selecting the 'closer'
1
4496
by: Rahul Babbar | last post by:
Hi, What are the possible ways to change the data type of a column from BigInt to Integer? the normal command Alter table <tabnamealter column <colnameset data type Integer gives the error SQL0190N ALTER TABLE <tablespecified attributes for column "VALUE" that are not compatible with the existing column. SQLSTATE=42837
0
9639
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
10311
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
10146
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...
0
9942
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...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.