471,337 Members | 988 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Question on "is" operator and casting

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.
--
Jim
Dec 5 '05 #1
3 1508
JimM <Ji**@noemail.nospam> wrote:
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.

--
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
Dec 5 '05 #2
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.

Dec 6 '05 #3
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.

Dec 8 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

231 posts views Thread by Brian Blais | last post: by
6 posts views Thread by M Welinder | last post: by
15 posts views Thread by Bobby C. Jones | last post: by
9 posts views Thread by Lonnie Princehouse | last post: by
17 posts views Thread by nicolas.hilaire | last post: by
81 posts views Thread by BlueJ774 | 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.