dynamic casting/creating

this isn't allowed :

if (soort.Equals("Form1")) {
Form1 formulier = (Form1)Sessie.cachedForms.Pop();
formulier.method_of_form1
formulier.show
}

else if (soort.Equals("Form2")
Form2 ...

Error 2 : Embedded statement cannot be a declaration or labeled statement


Sessie.cachedForms.Pop() returns a reference to a form. What i want to do is making a new Form of the correct type. If I don't cast it to the right form, it is not possible to access a method.

How to do this









Answer this question

dynamic casting/creating

  • jcarter

    I know it will :)

  • Thanes

    But I first worked also with a model of an abstract class but then I had problems in Visual Studio to view designer view, adding components...

    The analogy is exactly what I want to do.

    Is it possible to use the existing forms (thus without step two) and still viewing it in designer view

  • Ambrish.Mishra

    there are a few ways around this, depends on what the CachedForms have in common and what your pattern is.
    one way -
    • Create this form
    partial class CacheFormBase: Form
    {
        protected virtual void CacheFormMethod() 
       
            throw new NotImplementedException("DO NOT INSTANCE BASE CLASS"); 
       
        protected override OnLoad(EventArgs e) 
       
            CacheFormMethod(); 
        }
    }
    • Build your project. then add new Item, select "Inherited Form" and select "CacheFormBase" Save as Form1, Change code to this:
    partial class Form1: CacheFormBase
    {
        protected override void CacheFormMethod() 
       
            MessageBox.Show("Here is Method of Form1"); 
        }
    }
    • Repeat Step 2 save as Form 2
    partial class Form2: CacheFormBase

        protected override void CacheFormMethod() 
       
            MessageBox.Show("Here is Method of Form2"); 
        }
    }
    • move your conditional into CachedForm.Pop
    public class CachedForms

        public static CacheFormBase Pop(object formType) 
       
            if (formType.Equals("Form1")) 
                return new Form1(); 
            if (formType.Equals("Form2")) 
                return new Form2(); 
            throw new ArgumentException(string.format("{0} is not a valid Form Name", formType.ToString())); 
        }
    }
    • now your code is simply:
    CachedFormBase someform = Sessie.cachedForms.Pop(soort);
    someform.Show();

    The above aproach is an example of "Visual Inheritance"

    check this thread

    There are some more advanced OOP techniques to look at which would be more optimal, Specifically "Replace Conditional with Polymorphism" but I am not sure you want to go there.

     

     



  • Humpty

    so for the the designer view:

    you can have inherited forms but the base class may not have abstract methods

  • Eleda

    ahhh. the designer cant design abstract classes (it cant create an instance) that was the reason for me using a NotImplementedException in the virtual CacheFormMethod base instead of marking it abstract.

  • mattwink

    thank you, I'll take a look to it tomorow and tell you if it worked

  • Emmy008

    I just took a closer look at your original code. . . looks like you were missing a ')'

    else if (soort.Equals("Form2") ) // see the paren
    Form2 ...

    still take a look at the approach I outlined.

    here's an analogy:

    You go into a bar one day and order a martini, bartender prepares martini and serves back a beverage(It just so happens that it contains a martini because you placed an order that the bartender understood and you go to this bar because the bartender is competent and knows how to make martinis). you drink your beverage.

    You go into a bar the next day and order a gibson, bartender prepares gibson and serves back a beverage(again it contains a gibson because of a proper order to a competent bartender). you drink your beverage.

    Beverage beverage = Bartender.Prepare(BeverageType.Martini);
    this.Drink(beverage);

    Beverage beverage = Bartender.Prepare(BeverageType.Gibson);
    this.Drink(beverage);

    you don't do:

    if (this.Want(BeverageType.Martini))
    {
    MartiniBeverage beverage= Bartender.PrepareMartini();
    this.DrinkMartini(beverage);
    }
    else
    {
    GibsonBeverage beverage= Bartender.PrepareGibson();
    this.DrinkGibson(beverage)
    }


    Your behavior should be consistent becuase you are making the same actions. Ordering a beverage and drinking it. . .

    On the other hand, your behavior after drinking a few beverages might be totally inconsistent



  • Alphadon

    Blair Allen Stark wrote:
    I know it will :)

    except for the typo "ArgumentExcepeption". . . fixed now. . .

    cheers!



  • YunZhou_MS

    exactly!

  • dynamic casting/creating