Rufus
Rufus

Reputation: 121

"is"-reference or gettype()

I'm searching inside some controls in my WinForms Form, with the help of foreach statements. I'm comparing the object i find through a "is"-reference (a is DataGridView). With "a" being an object in a control collecion. That works fine so far, because the compared objects on my form are all sufficiantly different from one another.

In a new form i created i'm using a derived version of a DataGridView called my_datagridview. So when a my_datagridview is compared to a DataGridView through a "is"-reference no exception is thrown, which is "wrong" cause i want to handle the two seperately.

Is there a way to compare my_datagridview and DataGridView properly?

Upvotes: 2

Views: 224

Answers (6)

Anirudha
Anirudha

Reputation: 32807

Upcast always succeeds and downcast always fails!

So when you upcast my_datagridview to DataGridView it would always succeed!

Doing this would cause an InvalidCastException as the downcast fails!

DataGridView dgv = new DataGridView();
myDataGrivView m_dgv = (myDataGridView)dgv;

To avoid throwing of the above exception,you can use the as operator!

Instead of throwing exception it returns null if the downcast fails!

DataGridView dgv = new DataGridView();
myDataGrivView m_dgv =dgv as myDataGridView;

if(m_dgv==null)
{
//its a datagridview
}
else
{
//its a mydatagridview
}

Upvotes: 1

danish
danish

Reputation: 5600

Based on our comments above, I do not see a need to find the controls. For example, if you have a button on the form and by click that something should happen to grid1, you could use this in click event handler for the button:

private void ClickButtonOne(object sender, EventArgs e)
{
// Do something with datagridview here
}

Upvotes: 0

rerun
rerun

Reputation: 25505

first I like as better

so

var dg = a as DataGrindView
var mygd = a as MyDataGridView

if(mygd != null) {...}
else
{
   if(dg != null) {...}
}

Upvotes: 1

Colin Mackay
Colin Mackay

Reputation: 19175

Compare the more derived version first and perform its actions, then compare the less derived type (assuming that actions are mutually exclusive)

Alternatively, in put the two comparisons in one conditional statement:

if ((a is my_datagridview) && (!a is DataGridView))
{
  // This will only match your derived version and not the Framework version
}
// The else is needed if you need to do something else for the framework version.
else if (a is DataGridView)
{
  // This will only match the framework DataGridView because you've already handled
  // your derived version.
}

Upvotes: 0

Matt Burland
Matt Burland

Reputation: 45145

Yes. Start with the most specific class first. So:

if (a is my_datagridview)
{
    //....
}
else if (a is DataGridView)
{
    // ....
}

See MDSN here.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1501043

Is there a way to compare my_datagridview and DataGridView properly?

One option would be to use something like:

if (a is MyDataGridView) // Type name changed to protect reader sanity
{
}
else if (a is DataGridView)
{
    // This will include any subclass of DataGridView *other than*
    // MyDataGridView
} 

Or you could use GetType() to match exactly, of course. The important question is what you would want to happen with any other classes derived from DataGridView or even from MyDataGridView.

Upvotes: 6

Related Questions