Click here to Skip to main content
16,005,037 members
Home / Discussions / C#
   

C#

 
GeneralRe: convert delimited text file into database file using 2005 C# Pin
yefeng_law22-Oct-08 5:00
yefeng_law22-Oct-08 5:00 
GeneralRe: convert delimited text file into database file using 2005 C# Pin
Mark Salsbery22-Oct-08 6:15
Mark Salsbery22-Oct-08 6:15 
GeneralRe: convert delimited text file into database file using 2005 C# Pin
led mike22-Oct-08 7:10
led mike22-Oct-08 7:10 
GeneralRe: convert delimited text file into database file using 2005 C# Pin
Mark Salsbery22-Oct-08 7:17
Mark Salsbery22-Oct-08 7:17 
GeneralRe: convert delimited text file into database file using 2005 C# Pin
led mike22-Oct-08 8:20
led mike22-Oct-08 8:20 
QuestionSimple Interface Question Pin
tjh722-Oct-08 4:13
tjh722-Oct-08 4:13 
AnswerRe: Simple Interface Question Pin
SeMartens22-Oct-08 4:19
SeMartens22-Oct-08 4:19 
AnswerRe: Simple Interface Question PinPopular
Simon P Stevens22-Oct-08 4:26
Simon P Stevens22-Oct-08 4:26 
No you shouldn't.

I'll go through your code line by line and explain what is happening.

This line creates a new object of type ClassA and assigns it to the classA variable
ClassA classA = new ClassA();

This line takes that ClassA object and casts it into a IClass type variable. Note that the object itself is still a ClassA object, it's just being stored in a variable that is IClass
IClass iclass = (IClass)classA;
Now you have an IClass object, you can only call methods on it that are part of that interface. You can't call ClassA specific methods on it, but it is still a ClassA object, so when you call those IClass methods, they are routed to the ClassA definitions for execution.

What you are trying to do here is cast an IClass object as a ClassB object. but the object isn't of type ClassB, it's of type ClassA, so the cast fails
ClassB classB = (ClassB)iclass;

This would work, because the object is a ClassA object, so can be cast as such.
ClassA aAgain = (ClassA)iclass;
What your doing is like taking an apple, putting it in a box that is labelled "fruit", then trying to take the apple back out of the box and claim it is now an orange. Casting doesn't ever actually change the underlying type.

By the way, you don't actually need the first cast. because ClassA already inherits from IClass, you can just do the assignment without casting
IClass iclass = classA;


Simon

GeneralRe: Simple Interface Question Pin
SeMartens22-Oct-08 4:35
SeMartens22-Oct-08 4:35 
Questionhow to edit appconfig in windows application(C#)?? Pin
Piyush Vardhan Singh22-Oct-08 2:35
Piyush Vardhan Singh22-Oct-08 2:35 
AnswerRe: how to edit appconfig in windows application(C#)?? Pin
Simon P Stevens22-Oct-08 3:33
Simon P Stevens22-Oct-08 3:33 
AnswerCP IGNORE: cross poster and not in right forums Pin
leckey22-Oct-08 14:37
leckey22-Oct-08 14:37 
QuestionHow to know in run time if there is active microphone in current machine ? Pin
Yanshof22-Oct-08 2:26
Yanshof22-Oct-08 2:26 
AnswerRe: How to know in run time if there is active microphone in current machine ? Pin
Simon P Stevens22-Oct-08 3:37
Simon P Stevens22-Oct-08 3:37 
QuestionCalling a Help File in C# Pin
Vimalsoft(Pty) Ltd22-Oct-08 2:23
professionalVimalsoft(Pty) Ltd22-Oct-08 2:23 
QuestionMake list of properties extracted from list of more complex objects. Pin
ktest1234522-Oct-08 1:16
ktest1234522-Oct-08 1:16 
AnswerRe: Make list of properties extracted from list of more complex objects. Pin
N a v a n e e t h22-Oct-08 1:43
N a v a n e e t h22-Oct-08 1:43 
GeneralRe: Make list of properties extracted from list of more complex objects. Pin
ktest1234522-Oct-08 1:55
ktest1234522-Oct-08 1:55 
AnswerRe: Make list of properties extracted from list of more complex objects. [modified] Pin
Mirko198022-Oct-08 2:12
Mirko198022-Oct-08 2:12 
QuestionProblem with Data Reports in C#.Net Pin
Member 369523622-Oct-08 0:24
Member 369523622-Oct-08 0:24 
QuestionProblem while executing pps file from C# Pin
selcuks21-Oct-08 22:32
selcuks21-Oct-08 22:32 
AnswerRe: Problem while executing pps file from C# Pin
Giorgi Dalakishvili21-Oct-08 22:51
mentorGiorgi Dalakishvili21-Oct-08 22:51 
AnswerRe: Problem while executing pps file from C# Pin
Ashfield21-Oct-08 23:32
Ashfield21-Oct-08 23:32 
GeneralRe: Problem while executing pps file from C# [modified] Pin
selcuks22-Oct-08 0:25
selcuks22-Oct-08 0:25 
GeneralRe: Problem while executing pps file from C# Pin
Thomas Stockwell22-Oct-08 1:53
professionalThomas Stockwell22-Oct-08 1:53 

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.