473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3275
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
299
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in...
4
536
by: ichor | last post by:
hi what is the use of the typeof keyword , and how does it differ from the GetType method. i found the GetType method useful but i fail to understand the use of the typeof method. i have tried...
7
9934
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
950
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
1
1618
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' '...
11
5597
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
2327
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool...
4
7095
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the...
20
9243
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
20
21706
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof()...
0
7228
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,...
0
7128
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...
1
7058
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...
0
5635
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,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
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...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
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 ...

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.