locked
Get Parent and Cast of Its Type RRS feed

  • Question

  • I got two wpf forms (user controls MainFormA and MainFormB) and share the same subform (another user contorl SubFormC)

    MainFormA and MainFormB are similar in some parts

    I access the parent from SubFormC

    Grid grid_Form = (this.Parent as Grid);
    MainFormA mainFormA = (grid_Main.Parent as MainFormA);
    mainFormA.status = "Modified";

    so I can access variable and functions in mainFormA/B.

    In this case, I have to specify the parent to MainFormA. However, I would like to auto detect the type of parent

    is this must use two cases to do? or there is a more general way?

    Grid grid_Main = (this.Parent as Grid);
    if (grid_Main.Parent.GetType().ToString() == "MainFormA") {
    MainFormA mainForm = (grid_Main.Parent as MainFormA);
    mainForm.status = "Modified";
    ....
    } else {
    MainFormB mainForm = (grid_Main.Parent as MainFormB);
    mainForm.status = "Modified";
    ...
    }

    Thanks.



    • Edited by swivan Thursday, February 21, 2013 2:04 AM
    Thursday, February 21, 2013 1:41 AM

Answers

  • Well , you can use dynamic keyword which will resolve the problem for you as shown below:

    dynamic myForm = grid_Main.Parent; myForm.status = "WhateverTheStatus"; //Its as simple and you dont need to do any casting also.

    Hope that helps.

    Cheers !!

    Edited:

    On the other note , since you have two different classes with the same property called status , may be you can implement that interface which contain a single property called status on both the windows. That would be more efficient at runtime (performance wise) than dynamic keyword.

    • Proposed as answer by Avinash Rai Thursday, February 21, 2013 12:44 PM
    • Edited by Avinash Rai Thursday, February 21, 2013 12:47 PM Updated about another possible solution.
    • Marked as answer by swivan Friday, February 22, 2013 4:38 AM
    Thursday, February 21, 2013 12:44 PM

All replies

  • Above question is not actually related to WPF , nevertheless , using GetType() is the only method known to me to know actual type of the object.

    Could you explain what do you mean by auto detect ? ( do you dont wanna do any string comparision & then casting based on that or is it anything else bothering you?)

    Cheers !!

    Thursday, February 21, 2013 5:59 AM
  • Above question is not actually related to WPF , nevertheless , using GetType() is the only method known to me to know actual type of the object.

    Could you explain what do you mean by auto detect ? ( do you dont wanna do any string comparision & then casting based on that or is it anything else bothering you?)

    Cheers !!

    I am new to WPF so not know is the question related to WPF, sorry about that.

    Infact I want to reduce duplicated coding and write something like the following. grid_Main.Parent may be MainFormA or MainFormB. However, grid_Main.Parent do not have status here and I could not compiled the program.

    Grid grid_Main = (this.Parent as Grid);
    grid_Main.Parent.status = "Modified"; // <== Error, Parent have to be casted to MainFormA or MainFormB

    Auto detect means the subform will know the parent is MainFormA or MainFormB then cast it to either one, then do the action correspondingly.

    i.e. If grid_Main.Parent is MainFormA, mainFormA.status will change to Modified and similar for MainFormB.

    Problem appears now, MainFormA and MainFormB also have a variable status but grid_Main.Parent must be casted to MainFormA or MainFormB before mainForma.status or mainFormb.status could be compiled successfully so the coding is then duplicated.

    Grid grid_Main = (this.Parent as Grid);
    if (grid_Main.Parent.GetType().ToString() == "MainFormA") {
    MainFormA mainForma = (grid_Main.Parent as MainFormA);
    <--DUPLICATED CODING START-->
    mainForma.status = "Modified";
    ....
    <--DUPLICATED CODING END-->
    } else {
    MainFormB mainFormb = (grid_Main.Parent as MainFormB);
    <--DUPLICATED CODING START-->
    mainFormb.status = "Modified";
    ....
    <--DUPLICATED CODING END-->
    }

    Hope the above explanation would let you understand better on my questions and thanks again.

    Thursday, February 21, 2013 7:42 AM
  • Well , you can use dynamic keyword which will resolve the problem for you as shown below:

    dynamic myForm = grid_Main.Parent; myForm.status = "WhateverTheStatus"; //Its as simple and you dont need to do any casting also.

    Hope that helps.

    Cheers !!

    Edited:

    On the other note , since you have two different classes with the same property called status , may be you can implement that interface which contain a single property called status on both the windows. That would be more efficient at runtime (performance wise) than dynamic keyword.

    • Proposed as answer by Avinash Rai Thursday, February 21, 2013 12:44 PM
    • Edited by Avinash Rai Thursday, February 21, 2013 12:47 PM Updated about another possible solution.
    • Marked as answer by swivan Friday, February 22, 2013 4:38 AM
    Thursday, February 21, 2013 12:44 PM
  • Well , you can use dynamic keyword which will resolve the problem for you as shown below:

    dynamic myForm = grid_Main.Parent; myForm.status = "WhateverTheStatus"; //Its as simple and you dont need to do any casting also.

    Hope that helps.

    Cheers !!

    Edited:

    On the other note , since you have two different classes with the same property called status , may be you can implement that interface which contain a single property called status on both the windows. That would be more efficient at runtime (performance wise) than dynamic keyword.

    dynamic works.

    For the other solution, it would be graetful if you can give an simple example with snippet of code.

    There are some shared variable and some common control such as button or labels.


    • Edited by swivan Friday, February 22, 2013 4:42 AM
    Friday, February 22, 2013 4:42 AM