473,385 Members | 1,707 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,385 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 1627
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
6
by: M Welinder | last post by:
The title more or less says it all: in C99, is the value of INT_MIN % -1 well defined (when performed as signed integers) under the assumption of two-complement representation. Note, that...
15
by: Bobby C. Jones | last post by:
Is there an advantage to either of these methods, or is it simply a matter of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
9
by: Lonnie Princehouse | last post by:
There doesn't seem to be any way to customize the behavior of "is" as can be done for other operators... why not?
4
by: Johndow | last post by:
I have trouble to use "Is" operator to a type that is from reflection. Basically, here is what I want to do, if Class1 type is not from reflection: class Class1 { } public class Test { ... if...
17
by: nicolas.hilaire | last post by:
Hi all, i've read this article http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first interest me much. I've translated it to use generic instead of template : generic < typename ...
5
by: veaux | last post by:
I'm thinking this is easy but can't get it. I have a table with following: Table1 Date 1/1/2007 Table2 Type 0107 (This is MMYY of above) So I'm having trouble using a query to turn the...
81
by: BlueJ774 | last post by:
Can someone please explain to me the difference between the "is" keyword and the == boolean operator. I can't figure it out on my own and I can't find any documentation on it. I can't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.