Set form on the fly?

  • Thread starter Thread starter vb6dev
  • Start date Start date
V

vb6dev

Hi,

I have code such as:
Form01 f1 = new Form01(); where Form01 is a form in my project.
then I have Form02 f2 = new Form02();

etc. I have 50 forms. Can I use Form f = new Form(); and then assign
FormXX to f?

It must be simple, but I can't find how to assign one of my 50 forms to my
generic f form.

Thanks

vbdev
 
vb6dev

Try to forget the form handling from VB6.

It has changed also in VBNet and is for classic VB devellopers probably one
of the biggest problems, in VB6 was taken let say a non OO approach to make
RAD easy.

Try to leave that approach, it will only give you problems in future.

See all your different forms as seperated classes that should be handled as
every other class.

Just my thought

Cor
 
If you need a variable of Type Form to be used in a method you can just cast a type down as long as the class you are casting
inherits from Form or a casting operator has been overridden in either of the Types.

Here's an example of how this may be useful when you have a method that requires some common Type to do work:

public string GiveMeAForm(Form form)
{
// All Form Types (those classes that derive from Form) will have a Text property that they inherit from the Form Type.
return form.Text;
}

public static void Main()
{
// Create a new Form of a Type you have declared
MyForm form = new MyForm();

// There is an implicit cast occuring here since the framework can easily change your MyForm reference into a Form reference
since
// it inherits directly from this class.
Console.WriteLine(GiveMeAForm(form));
}


This is an extremely simple example meant to illustrate one use of inheritance in .NET. This code doesn't actually do anything
useful since in the case of Form you will have access to the Text property without requiring a method, but you can see how casting
can be useful in more complex situations.

You should research object oriented programming (OOP) and techniques, and polymorphism to get a better understanding of inheritance.
 
Back
Top