Click here to Skip to main content
15,914,066 members
Home / Discussions / C#
   

C#

 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Luc Pattyn9-Jul-07 14:18
sitebuilderLuc Pattyn9-Jul-07 14:18 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#9-Jul-07 8:51
Martin#9-Jul-07 8:51 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Colin Angus Mackay9-Jul-07 11:19
Colin Angus Mackay9-Jul-07 11:19 
GeneralRe: unable to cast object of type 'X' to type 'X' [modified] Pin
Martin#9-Jul-07 19:21
Martin#9-Jul-07 19:21 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#9-Jul-07 19:54
Martin#9-Jul-07 19:54 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Vikram A Punathambekar9-Jul-07 17:50
Vikram A Punathambekar9-Jul-07 17:50 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#9-Jul-07 19:35
Martin#9-Jul-07 19:35 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#9-Jul-07 19:52
Martin#9-Jul-07 19:52 
I just did the test project and found out what I allready assumed!

My interface:
public interface ITest
{
    bool PTest
    {
        get;
        set;
    }
}

My ClassA (derived from Control, implementing ITest plus an additional property)
public class CTest_A : System.Windows.Forms.Control, ITest
{
    public CTest_A()
    {
    }
    #region ITest Member
    private bool _ptest=false;
    public bool PTest
    {
        get
        {
            return _ptest;
        }
        set
        {
            _ptest = value;
        }
    }
    #endregion

    private bool _padditional_A=false;
    public bool PAdditional_A
    {
        get
        {
            return _padditional_A;
        }
        set
        {
            _padditional_A = value;
        }
    }
}

My ClassB (also derived from Control, also implementing ITest plus an additional property)
public class CTest_B : System.Windows.Forms.Control, ITest
{
    public CTest_B()
    {
    }

    #region ITest Member
    private bool _ptest=false;
    public bool PTest
    {
        get
        {
            return _ptest;
        }
        set
        {
            _ptest = value;
        }
    }
    #endregion

    private bool _padditional_B=false;
    public bool PAdditional_B
    {
        get
        {
            return _padditional_B;
        }
        set
        {
            _padditional_B = value;
        }
    }
}


My test class:
Has a an instance of CTest_B and whant's to cast it to CTest_A over the Interface, which is returned by a method!
CTest_B C_B = new CTest_B();

private ITest GetIReference()
{
    return C_B as ITest;
}

public Form1()
{
    InitializeComponent();

    CTest_A C_A = (CTest_A)GetIReference(); //Throws an invalid cast exception here
        }


Do I get something totaly wrong, or is it like I assumed?

Please help!

All the best,

Martin

GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Vikram A Punathambekar9-Jul-07 20:16
Vikram A Punathambekar9-Jul-07 20:16 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#9-Jul-07 20:25
Martin#9-Jul-07 20:25 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Vikram A Punathambekar9-Jul-07 22:08
Vikram A Punathambekar9-Jul-07 22:08 
GeneralRe: unable to cast object of type 'X' to type 'X' [modified] Pin
Martin#9-Jul-07 22:49
Martin#9-Jul-07 22:49 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Vikram A Punathambekar10-Jul-07 0:47
Vikram A Punathambekar10-Jul-07 0:47 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#10-Jul-07 1:18
Martin#10-Jul-07 1:18 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Vikram A Punathambekar10-Jul-07 4:30
Vikram A Punathambekar10-Jul-07 4:30 
GeneralRe: unable to cast object of type 'X' to type 'X' Pin
Martin#10-Jul-07 4:39
Martin#10-Jul-07 4:39 
AnswerRe: unable to cast object of type 'X' to type 'X' Pin
AFSEKI10-Jul-07 6:31
AFSEKI10-Jul-07 6:31 
QuestionSend a string to another program Pin
suntromantralalalala9-Jul-07 5:08
suntromantralalalala9-Jul-07 5:08 
AnswerRe: Send a string to another program Pin
Luc Pattyn9-Jul-07 6:51
sitebuilderLuc Pattyn9-Jul-07 6:51 
QuestionIterating through all values of an ORed enum variable... Pin
Shy Agam9-Jul-07 5:05
Shy Agam9-Jul-07 5:05 
AnswerRe: Iterating through all values of an ORed enum variable... Pin
Martin#9-Jul-07 5:39
Martin#9-Jul-07 5:39 
AnswerRe: Iterating through all values of an ORed enum variable... Pin
Luc Pattyn9-Jul-07 7:08
sitebuilderLuc Pattyn9-Jul-07 7:08 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
PhilDanger9-Jul-07 7:20
PhilDanger9-Jul-07 7:20 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Shy Agam9-Jul-07 8:12
Shy Agam9-Jul-07 8:12 
GeneralRe: Iterating through all values of an ORed enum variable... Pin
Luc Pattyn9-Jul-07 8:20
sitebuilderLuc Pattyn9-Jul-07 8:20 

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.