473,394 Members | 1,797 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,394 software developers and data experts.

A small problem comparing types

Hi all,

I hope someone can help me with the following:

I have a number of usercontrols that I've made which provides certain audit
functions for any data inserted into it. Each audit control is a descendant
of BaseAuditControl. Descedants include AuditableTextControl and
AuditableBoolControl.

The problem I'm having is determining which control I'm actually dealing
with at any one time. I often need to treat them generically for most of a
method and then need to implemement some logic that is specific to the
particlar type of audit control.

An example:

To update the main ui element of my audit control I make a method in my base
control. I need to be able to get the type of the current instance that I'm
dealing with, via the 'this' keyword, and then compare it to the various
types of AuditControl that I have made. Something like this:
if((this.GetType()) == (typeof(AuditableTextControl)){
//Do one thing
return false;
}

if((this.GetType()) == (typeof(AuditableBoolControl)){
//Do another
return false;
}

The problem I'm getting is that the Types returned from each of the two
expressions are different.

this.GetType() returns something like ASP.AuditableTextControl_ascx
typeof(AuditableTextControl) returns
<mynamespace>.controls.AuditableTextControl

I don't understand whats going on behind the scenes, but it seems to me that
when you get the type of an actual object instance, it is different from
trying to get the type using just the class definition.

If anyone can help me figure out how to get a successful comparison between
them. I've tried to make a fake instance of an AuditableTextControl and then
tried to get the type of that, but that doesnt work either. Seems a bit
messy as well. I'm sure that there is a really easy way to day this

Thanks to anyone who can help with this.

Take care

Simon
Nov 16 '05 #1
3 1480
Simon Harvey <sh856531@microsofts_free_email_service.com> wrote:
I hope someone can help me with the following:

I have a number of usercontrols that I've made which provides certain audit
functions for any data inserted into it. Each audit control is a descendant
of BaseAuditControl. Descedants include AuditableTextControl and
AuditableBoolControl.

The problem I'm having is determining which control I'm actually dealing
with at any one time. I often need to treat them generically for most of a
method and then need to implemement some logic that is specific to the
particlar type of audit control.

An example:

To update the main ui element of my audit control I make a method in my base
control. I need to be able to get the type of the current instance that I'm
dealing with, via the 'this' keyword, and then compare it to the various
types of AuditControl that I have made. Something like this:
if((this.GetType()) == (typeof(AuditableTextControl)){
//Do one thing
return false;
}

if((this.GetType()) == (typeof(AuditableBoolControl)){
//Do another
return false;
}
That's usually not a good way of working - you should use polymorphism
instead, usually.
The problem I'm getting is that the Types returned from each of the two
expressions are different.

this.GetType() returns something like ASP.AuditableTextControl_ascx
typeof(AuditableTextControl) returns
<mynamespace>.controls.AuditableTextControl
Right - that's because the control itself is generated from the ascx
file, and (I believe - I haven't created any user controls myself) only
derives from your actual user control class.
I don't understand whats going on behind the scenes, but it seems to me that
when you get the type of an actual object instance, it is different from
trying to get the type using just the class definition.

If anyone can help me figure out how to get a successful comparison between
them. I've tried to make a fake instance of an AuditableTextControl and then
tried to get the type of that, but that doesnt work either. Seems a bit
messy as well. I'm sure that there is a really easy way to day this


Using the more normal form of type comparison - the "is" operator - may
well work, if the ..._ascx type really does inherit from
AuditableTextControl.

if (this is AuditableTextControl)
{
....
}

Once again though, I'd suggest using polymorphism - define a virtual
(or abstract) method in the base control, and override it in each
derived class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Try using the IS operator to check for type comparison and AS operator for
casting

if (this IS AuditableTextControl)

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi all,

I hope someone can help me with the following:

I have a number of usercontrols that I've made which provides certain audit functions for any data inserted into it. Each audit control is a descendant of BaseAuditControl. Descedants include AuditableTextControl and
AuditableBoolControl.

The problem I'm having is determining which control I'm actually dealing
with at any one time. I often need to treat them generically for most of a
method and then need to implemement some logic that is specific to the
particlar type of audit control.

An example:

To update the main ui element of my audit control I make a method in my base control. I need to be able to get the type of the current instance that I'm dealing with, via the 'this' keyword, and then compare it to the various
types of AuditControl that I have made. Something like this:
if((this.GetType()) == (typeof(AuditableTextControl)){
//Do one thing
return false;
}

if((this.GetType()) == (typeof(AuditableBoolControl)){
//Do another
return false;
}

The problem I'm getting is that the Types returned from each of the two
expressions are different.

this.GetType() returns something like ASP.AuditableTextControl_ascx
typeof(AuditableTextControl) returns
<mynamespace>.controls.AuditableTextControl

I don't understand whats going on behind the scenes, but it seems to me that when you get the type of an actual object instance, it is different from
trying to get the type using just the class definition.

If anyone can help me figure out how to get a successful comparison between them. I've tried to make a fake instance of an AuditableTextControl and then tried to get the type of that, but that doesnt work either. Seems a bit
messy as well. I'm sure that there is a really easy way to day this

Thanks to anyone who can help with this.

Take care

Simon

Nov 16 '05 #3
Hi Simon,

Make sure that you specify the className attribute in @Control directive in
the .ascx file implementing your UserControl.

If you don't specify this attribute when you create an instance of a
control, then framework appends _ascx to the class name of that control,
defined in codebehind source file, and assigns it to the control. Eg:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="MyControl.ascx.cs" classname="MyControl"
Inherits="MyAspApp.Controls.MyControl">

In the above, if classname=MyControl is omitted, then the strong name would
have _ascx appended.

Thanks and Regards,
GirishKumar
ICICI Infotech
"Simon Harvey" wrote:
Hi all,

I hope someone can help me with the following:

I have a number of usercontrols that I've made which provides certain audit
functions for any data inserted into it. Each audit control is a descendant
of BaseAuditControl. Descedants include AuditableTextControl and
AuditableBoolControl.

The problem I'm having is determining which control I'm actually dealing
with at any one time. I often need to treat them generically for most of a
method and then need to implemement some logic that is specific to the
particlar type of audit control.

An example:

To update the main ui element of my audit control I make a method in my base
control. I need to be able to get the type of the current instance that I'm
dealing with, via the 'this' keyword, and then compare it to the various
types of AuditControl that I have made. Something like this:
if((this.GetType()) == (typeof(AuditableTextControl)){
//Do one thing
return false;
}

if((this.GetType()) == (typeof(AuditableBoolControl)){
//Do another
return false;
}

The problem I'm getting is that the Types returned from each of the two
expressions are different.

this.GetType() returns something like ASP.AuditableTextControl_ascx
typeof(AuditableTextControl) returns
<mynamespace>.controls.AuditableTextControl

I don't understand whats going on behind the scenes, but it seems to me that
when you get the type of an actual object instance, it is different from
trying to get the type using just the class definition.

If anyone can help me figure out how to get a successful comparison between
them. I've tried to make a fake instance of an AuditableTextControl and then
tried to get the type of that, but that doesnt work either. Seems a bit
messy as well. I'm sure that there is a really easy way to day this

Thanks to anyone who can help with this.

Take care

Simon

Nov 16 '05 #4

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

Similar topics

88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
5
by: George | last post by:
How do I compare the values of two objects when Option Strict is On? One of the objects is the Tag property from some control (declared as object but holding a value, e.g. an Integer or a String...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
8
by: Joe | last post by:
Hi trying to write some code to determine if user has large/small fonts set on PC thought this would work but it didn't Any suggestions??? Thanks Graphics g = Graphics.FromHwnd(this.Handle);
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
3
by: Mike J | last post by:
Hi..need help comparing types example method istypeof(object someobj) { if (someobj=int32) } ya kinda get my idea here.... MJ
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
0
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,...
0
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...
0
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
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...

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.