Question on "is" operator and casting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to create a method in VS 2003 that validates an object argument
is of the proper type and within a range of values.

I am trying to use a Type to define the casting and object type for the
validation. But I get and error of "The type or namespace name 't' could not
be found (are you missing a using directive or and assembly reference?) on
the "is" operator and the casting operation.

An example of the method follows. The "is" operator is defined as

expression is type

"t" is a type.

private bool Validate( object data, object min, object max, Type t )
{
if( data != null && data is t )
{
if( ( t )data >= ( t )min || ( t )data <= ( t )max )
{
return true;
}
}
return false;
}

Is their any way to do this in VS 2003. I know I can use generics with
2005, but hat is not an option at this time.
 
JimM said:
I am trying to create a method in VS 2003 that validates an object argument
is of the proper type and within a range of values.

I am trying to use a Type to define the casting and object type for the
validation. But I get and error of "The type or namespace name 't' could not
be found (are you missing a using directive or and assembly reference?) on
the "is" operator and the casting operation.

Yes, that's because the "is" operator and casting work on the type
known at *compile* time, not runtime.
An example of the method follows. The "is" operator is defined as

expression is type

"t" is a type.

private bool Validate( object data, object min, object max, Type t )
{
if( data != null && data is t )

Here you can use

if (data != null && t.IsAssignableFrom(data))
{
if( ( t )data >= ( t )min || ( t )data <= ( t )max )
{
return true;
}
}
return false;
}

That's rather different (and generics wouldn't help you either). I
strongly suggest that instead of passing in a type, you make the data
implement IComparable, and call CompareTo on it, passing in Min and
Max.
 
Hi Jim,

Thanks for your post.

I think "Jon Skeet [C# MVP]" has provided you the correct information.

First, Type.IsAssignableFrom Method returns true if the c parameter and the
current Type represent the same type, or if the current Type is in the
inheritance hierarchy of c, or if the current Type is an interface that c
supports. So it should meet your need of determine if the object can be
assigned to this type. However, this does not contain the casting, if you
want to cover the casting scenario, I think Convert.ChangeType() method is
your friend.

Second, I agree with "Jon Skeet [C# MVP]" that if you want to implement a
method accepting polymorphism object type for comparison, the object should
at least implement IComparable interface. Only an object implemented
IComparable interface, can it make sense for comparison in a range. In
current .Net FCL, almost all the primitive types implemented this
interface, such as System.Int32, System.Int16, System.UInt32,
System.UInt16, etc... You can use Reflector to view "Derived Types" node
under IComparable interface to get all the FCL classes implemented
IComparable interface. For your own customized class type, this class
should also implement IComparable interface for comparison.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jim,

Does my our reply make sense to you? If you still have any concern, please
feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top