Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
QuestionC# Pin
arindam201016-Sep-10 1:23
arindam201016-Sep-10 1:23 
AnswerRe: C# Pin
OriginalGriff16-Sep-10 1:54
mveOriginalGriff16-Sep-10 1:54 
GeneralMessage Removed Pin
16-Sep-10 2:08
arindam201016-Sep-10 2:08 
GeneralRe: C# Pin
Not Active16-Sep-10 2:17
mentorNot Active16-Sep-10 2:17 
AnswerInterview question Pin
Not Active16-Sep-10 2:16
mentorNot Active16-Sep-10 2:16 
GeneralRe: Interview question Pin
Luc Pattyn16-Sep-10 2:25
sitebuilderLuc Pattyn16-Sep-10 2:25 
GeneralRe: Interview question Pin
Not Active16-Sep-10 2:59
mentorNot Active16-Sep-10 2:59 
AnswerRe: C# [modified] Pin
OriginalGriff16-Sep-10 5:19
mveOriginalGriff16-Sep-10 5:19 
Since you deleted your question, I'll reply here. (Please don't delete questions, it leaves the forum with half a conversation - that doesn't help anyone else who has a similar problem)


arindam2010 wrote:
Can you please tell me little bit more about casting..??

the code works if i write like this

B obj1 = (B)new A();

what is happening actually here ??


First off, it won't work if you write it like that - it will compile, because you are using an explicit cast. But when you try to run it, you will get a run-time exception along the lines of "unable to cast from type A to type B" and your program will stop. Why? Because it would need to invent information, and it doesn't know how to do that.
When you cast from an integer to a float, it knows what to do: add "0.0" and call it a float! When you cast from a float to an integer, it knows what to do: Throw away any digits to the right of the decimal point.
When you cast from one of your classes to another, it doesn't know what is happening. It knows they are different, but it doesn't know how to make them the same.
Think of it this way: If you have a Ford Ka, and fit a Ferrari badge instead of the blue oval, will it out accelerate a Porche? No - it is still a Ford, but it looks a bit different - and won't fool anyone.
You can down cast from a derived class to the class it derived from (because everything in the source class is still there in the derived class), and you can cast that back to the derived class (because it started out as the derived class and in real terms, it still is - you just changed the name).
This is a bit complex to explain without face to face and maybe some diagrams - I would suggest you discuss this with your tutor as he/she will have explained this many times before!

[edit] Blush | :O Left? Right? My right hands side doesn't know whet my left hand side is doing... Blush | :O [/edit]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
modified on Thursday, September 16, 2010 2:50 PM

GeneralRe: C# Pin
Chris Trelawny-Ross16-Sep-10 8:42
Chris Trelawny-Ross16-Sep-10 8:42 
GeneralRe: C# Pin
OriginalGriff16-Sep-10 8:50
mveOriginalGriff16-Sep-10 8:50 
GeneralRe: C# Pin
Pete O'Hanlon16-Sep-10 10:08
mvePete O'Hanlon16-Sep-10 10:08 
GeneralRe: C# Pin
OriginalGriff16-Sep-10 21:23
mveOriginalGriff16-Sep-10 21:23 
AnswerRe: C# Pin
#realJSOP16-Sep-10 23:36
mve#realJSOP16-Sep-10 23:36 
QuestionCreate XML using XSD Pin
NarVish15-Sep-10 23:36
NarVish15-Sep-10 23:36 
AnswerRe: Create XML using XSD Pin
Pete O'Hanlon16-Sep-10 1:31
mvePete O'Hanlon16-Sep-10 1:31 
GeneralRe: Create XML using XSD Pin
NarVish16-Sep-10 1:41
NarVish16-Sep-10 1:41 
GeneralRe: Create XML using XSD Pin
Pete O'Hanlon16-Sep-10 2:05
mvePete O'Hanlon16-Sep-10 2:05 
GeneralRe: Create XML using XSD Pin
NarVish16-Sep-10 2:36
NarVish16-Sep-10 2:36 
AnswerRe: Create XML using XSD Pin
Bernhard Hiller16-Sep-10 2:32
Bernhard Hiller16-Sep-10 2:32 
GeneralRe: Create XML using XSD Pin
NarVish16-Sep-10 3:03
NarVish16-Sep-10 3:03 
GeneralRe: Create XML using XSD Pin
Bernhard Hiller16-Sep-10 4:23
Bernhard Hiller16-Sep-10 4:23 
Questionc# Code problem I can't find out Pin
C.CoderCreator15-Sep-10 23:33
C.CoderCreator15-Sep-10 23:33 
AnswerRe: c# Code problem I can't find out Pin
Calla15-Sep-10 23:50
Calla15-Sep-10 23:50 
GeneralRe: c# Code problem I can't find out Pin
C.CoderCreator16-Sep-10 0:02
C.CoderCreator16-Sep-10 0:02 
GeneralRe: c# Code problem I can't find out Pin
Calla16-Sep-10 0:58
Calla16-Sep-10 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.