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

dynamic casting/creating
jcarter
Thanes
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
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: 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: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
you can have inherited forms but the base class may not have abstract methods
Eleda
mattwink
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.
you don't do:
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
except for the typo "ArgumentExcepeption". . . fixed now. . .
cheers!
YunZhou_MS