A small problem comparing types

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

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
 
Simon Harvey said:
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.
 
Try using the IS operator to check for type comparison and AS operator for
casting

if (this IS AuditableTextControl)
 
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
 
Back
Top