473,378 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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_DragDrop(object sender,
System.Windows.Forms.DragEventArgs 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 7105
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.com
"Andy" <do**@spam.me> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
I'm making a drag and drop application, and in the *_DragDrop(...)-method
I
have the following code:

private void SelectTree_DragDrop(object sender,
System.Windows.Forms.DragEventArgs 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 AbstractNodeWrapper
{
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.com

"Andy" <do**@spam.me> wrote in message
news:OR*************@TK2MSFTNGP10.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 AbstractNodeWrapper
{
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_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
string type = e.Data.GetFormats().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(AbstractNode) 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.com
"Andy" <do**@spam.me> wrote in message
news:eY**************@TK2MSFTNGP15.phx.gbl...
[Serializable]
public struct AbstractNodeWrapper
{
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_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
string type = e.Data.GetFormats().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(AbstractNode) 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
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"...
6
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...
13
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...
4
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...
13
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...
4
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
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...
4
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...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.