C Board  

Go Back   C Board > General Programming Boards > C# Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 05-04-2008, 03:41 AM   #1
Registered User
 
Join Date: May 2006
Posts: 1,579
compile time error or runtime error?

Hello everyone,


Could anyone let me know why compiler can not make runtime error report in the following code, other than throw cast exception during runtime?

Code:
    static void Main()
    {
        int i = 100;
        object b = i;
        long l = (long)b; // why compiler can not make type check here?

        return;
    }

thanks in advance,
George
George2 is offline  
Old 05-04-2008, 03:56 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Did you mean to say: "why compiler can not make compile time error report in the following code"?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline  
Old 05-04-2008, 05:21 AM   #3
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Because b is an "object" which can be a "long" (which is valid cast) but also something else (which might be invalid cast). The compiler does not know, and far from all cases are as easy to detect as the one you show.
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline  
Old 05-07-2008, 07:08 AM   #4
Registered User
 
Join Date: May 2006
Posts: 1,579
Thanks laserlight and Magos,


Are there any documents about why type cast/conversion are checked by C# compiler? :-)

Quote:
Originally Posted by Magos View Post
Because b is an "object" which can be a "long" (which is valid cast) but also something else (which might be invalid cast). The compiler does not know, and far from all cases are as easy to detect as the one you show.

regards,
George
George2 is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner Needs help in Dev-C++ Korrupt Lawz C++ Programming 18 05-02-2009 12:27 PM
Another syntax error caldeira C Programming 31 09-05-2008 01:01 AM
Post... maxorator C++ Programming 12 10-11-2005 08:39 AM
ras.h errors Trent_Easton Windows Programming 8 07-15-2005 10:52 PM
Linking error DockyD C++ Programming 10 01-20-2003 05:27 AM


All times are GMT -6. The time now is 05:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22