Click here to Skip to main content
15,912,457 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problems with combo box Pin
sinosoidal30-Mar-07 3:15
sinosoidal30-Mar-07 3:15 
Questionrepeater problem Pin
jaganil30-Mar-07 0:27
jaganil30-Mar-07 0:27 
AnswerRe: repeater problem Pin
J$30-Mar-07 11:11
J$30-Mar-07 11:11 
Questionobject oriented doubt Pin
kalyan_241630-Mar-07 0:23
kalyan_241630-Mar-07 0:23 
AnswerRe: object oriented doubt Pin
Colin Angus Mackay30-Mar-07 0:27
Colin Angus Mackay30-Mar-07 0:27 
GeneralRe: object oriented doubt Pin
kalyan_241630-Mar-07 0:35
kalyan_241630-Mar-07 0:35 
GeneralRe: object oriented doubt Pin
Colin Angus Mackay30-Mar-07 1:24
Colin Angus Mackay30-Mar-07 1:24 
AnswerRe: object oriented doubt Pin
Christian Graus30-Mar-07 3:18
protectorChristian Graus30-Mar-07 3:18 
FOr this to work at all, class2 needs to derive from class1. It doesn't matter if the functions in both classes are the same, if they have no inheritance relationship, you simply cannot cast them.

class1
{
public void Foo();
}

class2
{
public void Foo();
}

This code does not provide classes that can be cast to each other. There is no relationship.


public class1
{
public void Foo();
}


public class2 : class1
{
}

NOW class2 is of type class1. You can do this:

class2 c = new class2();
class1 c1 = (class1)c;

Note some differences :

1 - I cannot make a class1 into a class2, it is NOT a class2. class2 IS a class1, but it is more than that ( although I have not added any methods, it's automatic ).

2 - There was no need to cast c into a class2, it IS a class2. I am not sure if I needed to cast at all, I did just to be safe.

Another thing: You should really never do this:

class2 c2 = (class2) c;

This can throw exceptions at runtime. Instead, do this:

class2 c2 = c as class2;

This will assign c2 in all instances, if the cast fails, it will assign it as null. So, you add a check for null before you continue, to see if the cast was valid.

if (c2 != null)
{
// cast was valid.
}


Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

Questionfiltering data for dataset that is bound to grid Pin
JacquesDP29-Mar-07 23:58
JacquesDP29-Mar-07 23:58 
AnswerRe: filtering data for dataset that is bound to grid Pin
gauthee30-Mar-07 0:08
gauthee30-Mar-07 0:08 
GeneralRe: filtering data for dataset that is bound to grid Pin
JacquesDP30-Mar-07 0:13
JacquesDP30-Mar-07 0:13 
GeneralRe: filtering data for dataset that is bound to grid Pin
kalyan_241630-Mar-07 0:39
kalyan_241630-Mar-07 0:39 
GeneralRe: filtering data for dataset that is bound to grid Pin
JacquesDP30-Mar-07 1:20
JacquesDP30-Mar-07 1:20 
GeneralRe: filtering data for dataset that is bound to grid Pin
gauthee30-Mar-07 1:34
gauthee30-Mar-07 1:34 
QuestionSome data Decrypt to return error Length of the data to decrypt is invalid Pin
alpaslanaykovan29-Mar-07 23:57
alpaslanaykovan29-Mar-07 23:57 
AnswerRe: Some data Decrypt to return error Length of the data to decrypt is invalid Pin
Ennis Ray Lynch, Jr.30-Mar-07 4:04
Ennis Ray Lynch, Jr.30-Mar-07 4:04 
QuestionInheritance and reflection Pin
Russell Jones29-Mar-07 23:15
Russell Jones29-Mar-07 23:15 
AnswerRe: Inheritance and reflection Pin
Stefan Troschuetz29-Mar-07 23:36
Stefan Troschuetz29-Mar-07 23:36 
GeneralRe: Inheritance and reflection Pin
Russell Jones30-Mar-07 0:00
Russell Jones30-Mar-07 0:00 
AnswerRe: Inheritance and reflection Pin
DavidNohejl29-Mar-07 23:59
DavidNohejl29-Mar-07 23:59 
GeneralRe: Inheritance and reflection Pin
Russell Jones30-Mar-07 0:40
Russell Jones30-Mar-07 0:40 
GeneralRe: Inheritance and reflection Pin
DavidNohejl30-Mar-07 0:54
DavidNohejl30-Mar-07 0:54 
GeneralRe: Inheritance and reflection Pin
Russell Jones30-Mar-07 2:14
Russell Jones30-Mar-07 2:14 
QuestionProblems when removing from ListBox Pin
sinosoidal29-Mar-07 22:56
sinosoidal29-Mar-07 22:56 
AnswerRe: Problems when removing from ListBox Pin
Russell Jones29-Mar-07 23:30
Russell Jones29-Mar-07 23:30 

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.