RE: InvalidCastException

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Ilya Tumanov [MS] (ilyatum_at_online.microsoft.com)
Date: 04/22/04


Date: Thu, 22 Apr 2004 18:07:38 GMT

I see... Well, as I've explained in my previous post, you can not just cast
one enum to another.

You will get an enum with the same underlying numeric value, but the
meaning might be totally different.
In many cases there's no meaning at all.
For example, you can go ahead and cast System.Data.AcceptRejectRule to
System.IO.FileAccess, but it will accomplish nothing.

In this case there's a logical link between DbType and SqlDbType.
However, casting one to another won't work because underlying numeric
values are different for the same logical values.
It's up to you to connect them on a logical level. You have to look up
matching values using table or switch statement. Good luck.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> Thread-Topic: InvalidCastException
> thread-index: AcQoWRymUuqIP2aRQe2iG3XKXcwM3g==
> X-WN-Post: microsoft.public.dotnet.framework.compactframework
> From: "=?Utf-8?B?QXJpcw==?=" <anonymous@discussions.microsoft.com>
> References: <66A62F8B-6B6F-4D0D-B68D-10D307E5A657@microsoft.com>
<uzLhbB9JEHA.3088@cpmsftngxa10.phx.gbl>
> Subject: RE: InvalidCastException
> Date: Thu, 22 Apr 2004 04:01:07 -0700
> Lines: 1
> Message-ID: <8E1A3865-5E89-42E2-83C1-4BAD4128AFF5@microsoft.com>
> MIME-Version: 1.0
> Content-Type: text/plain;
> charset="Utf-8"
> Content-Transfer-Encoding: 7bit
> X-Newsreader: Microsoft CDO for Windows 2000
> Content-Class: urn:content-classes:message
> Importance: normal
> Priority: normal
> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
> Newsgroups: microsoft.public.dotnet.framework.compactframework
> Path: cpmsftngxa10.phx.gbl
> Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:51509
> NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
> X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
>
> What am I trying to do is to build an application that is database
independent. That is my application uses IdbCommand, IdbDataReader etc...
and in a switch statement I retrieve the name of the database from a
configuration file.So when I pass a DbType in a IdbParameter, I cast it as
an SqlDbType in a SqlParameter.
>



Relevant Pages

  • Re: Why do enum values req explicit conversions
    ... that an enum is definitely an instance of System.Enum - proven by the fact ... I suppose the most important issue is that the syntax of the cast operator ... is actually an unbox operation is incorrect. ... If it were already an int, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is there is keyword in C++
    ... Using C++ run-time type information you can try and cast the object to ... an object pointer of the desired type using dynamic_cast. ... - provided your class derives from CObject. ... and call a member function which would return the enum. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Is there is keyword in C++
    ... Using C++ run-time type information you can try and cast the object to ... an object pointer of the desired type using dynamic_cast. ... - provided your class derives from CObject. ... and call a member function which would return the enum. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: enum as index for DataRow
    ... public constant int Table1.Field1 = 0; ... > from myfields to int. ... > I REALLY do not want to have to cast every use of this enum. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler resolves to wrong overloaded constructor or method
    ... > Entirely correct, since such a cast can lose information, even though it ... guess would be that the Enum cast doesn't check type, ... > compiler implemenation, rather than being derivable from the language ... I don't *think* there is anything in the spec that defines 0.0 as ...
    (microsoft.public.dotnet.languages.csharp)