get me a new form

ajlajlajl

Member
Joined
Dec 3, 2009
Messages
5
Location
Yazd
Programming Experience
3-5
hi.
i have a problem with that.
my app has a group of forms that do a specific work in different ways.
i want to create a new variable of a form when i want with its name.
so. i want a Function in this form :
VB.NET:
Expand Collapse Copy
Public Function GetNew (byref f as form ) as form
that give a form name and return a variable of that type.
in other words, it must work as "dim f1 as new formx".
and, how i must do that?
tnx.
 
that give a form name
means this signature:
VB.NET:
Expand Collapse Copy
Public Function GetNew (ByVal form as [U][B]String[/B][/U]) As Form
To create and return different form instances you can simply use a Select Case statement. Closer Look: Using Select Case to Decide Between Multiple Choices

Also notice the method parameter should be ByVal (default), it is very rare to use ByRef parameters.
 
thanku JohnH.
but i can`t use "Select Case" because i haven`t the forms when writing that function. the function (GetNew) declared separately .and used in some projects . (so i want to create a var from an unknown data type?).
now, in my projects. i'm using select case for that purpose and must rewrite the function for any project and any form adds.tiresome.
and for Byref. when it have type of Form, no matter. only an address copies(or not).
winking0014.gif

tnx.
 
(so i want to create a var from an unknown data type?).
Then you have to look into Reflection. This allows creating an instance from it's string type name. Search "vb.net reflection" etc.
and for Byref. when it have type of Form, no matter. only an address copies(or not).
Even then it matters, because ByRef will allow the method to change the object reference of the caller, something that is not code safe when not intended.
 
thanks, thanks so mush.
i solved that by this :
VB.NET:
Expand Collapse Copy
    Public Function GetNew(ByVal f As Type) As Form
        Return Activator.CreateInstance(f)
    End Function
 
Back
Top