471,603 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to use TypeOf()?

I have a collection that holds DataGridTextBoxColumn types, which I
then bind to a DataGrid. I loop through all of the
DataGridTextBoxColumn that were created and add them to the collection
like this

for(int i=0; i<dataGridStyleColumnCollection.Count; i++)
{

gridTableStyle.GridColumnStyles.Add((DataGridTextB oxColumn)dataGridStyleColumnCollection[i]);
}

Sometimes, I'll have a DataGridBoolColumn, which breaks the above code.
I'm trying to put in a conditional that checks the type. This always
gives a warning:

The given expression is never of the provided
('System.Windows.Forms.DataGridBoolColumn') type

[the expression]
if(dataGridStyleColumnCollection[i].GetType() is DataGridTextBoxColumn)

gridTableStyle.GridColumnStyles.Add((DataGridTextB oxColumn)dataGridStyleColumnCollection[i]);

[In the debugger while looping]
- dataGridStyleColumnCollection[i].GetType() {System.RuntimeType} System.RuntimeType

+ System.Type {"System.Windows.Forms.DataGridBoolColumn"} System.Type

The debugger shows it is of the provided type. Neither of the above
conditionals ever run. They always evaluate to false. What does the
compiler and debugger say two different things?

Should I use something else to determine the column type?

Thanks,
Brett

Jan 12 '06 #1
1 3219
Brett Romero <ac*****@cygen.com> wrote:
I have a collection that holds DataGridTextBoxColumn types, which I
then bind to a DataGrid. I loop through all of the
DataGridTextBoxColumn that were created and add them to the collection
like this
<snip>
[the expression]
if(dataGridStyleColumnCollection[i].GetType() is DataGridTextBoxColumn)
<snip>
The debugger shows it is of the provided type. Neither of the above
conditionals ever run. They always evaluate to false. What does the
compiler and debugger say two different things?


Because what you're asking is whether the type System.Type is an
instance of DataGridTextColumnBox, which it never is. You're sort of in
a half-way house between:

if(dataGridStyleColumnCollection[i].GetType() ==
typeof(DataGridTextBoxColumn))

and

if(dataGridStyleColumnCollection[i] is DataGridTextBoxColumn)

Either of those would work (although the first one won't "match" if you
give it an instance of something derived from DataGridTextBoxColumn.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Eric | last post: by
4 posts views Thread by ichor | last post: by
3 posts views Thread by Alberto | last post: by
1 post views Thread by Brien King | last post: by
11 posts views Thread by Jason Kendall | last post: by
2 posts views Thread by Andrew Robinson | last post: by
20 posts views Thread by rkk | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

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.