send the class type as a parameter.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

Hello
I have a function, that I want to pass it via parameter a classType (object
type, or whatever it is called).
I.e I want to send a type : Button, Form, radioButton, etc.

Thanks :)
 
Am 24.04.2010 12:01, schrieb Mr. X.:
Hello
I have a function, that I want to pass it via parameter a classType (object
type, or whatever it is called).
I.e I want to send a type : Button, Form, radioButton, etc.

Sub bla(byval t as type) 'which is As System.Type

if t is gettype(button) then
elseif t is gettype(form) then
'...
end if

end sub

Call:
bla(gettype(Form))
 
1. What I want to do is to make a property, that is a Type (But only type of
Form).
I couldn't find how doing so (I can check on code, but I want also this type
to be checked at design time).
2. I want that any child components of forms will work too - How can I check
that ?
(Is the code, as you described OK :
if t is gettype(form) ' t is a child of form. does this line works too ?)

Thanks :)
 
Sorry.
If t is a form, and any of it's child components ?

I want to do as following :
dim x as t
if t is getType(form) then
x = new Form()
....
x.ShowDialog() ...
end if

How can I do that ?
(Maybe solution for "type", should be "Class" ?)

Thanks :)
 
Sorry.
If t is a form, and any of it's child components ?

I want to do as following :
dim x as t
if t is getType(form) then
x = new Form()
...
x.ShowDialog() ...
end if

How can I do that ?
(Maybe solution for "type", should be "Class" ?)

Thanks :)

Inside the if-then-else you know it is a form, so do the coding as follows:

sub bla (byval t as type)
if (t is gettype(Form)) then
dim x as new Form
x.ShowDialog()
end if
end sub
 
As "Family Tree Mike" said:
Inside the if-then-else you know it is a form, so do the coding as
follows:

sub bla (byval t as type)
if (t is gettype(Form)) then
dim x as new Form
x.ShowDialog()
end if
end sub

The problem is :
What t parameter has related to the code you gave ?
(The only occurrence is its declaration : byval t as type).

Thanks :)
 
As "Family Tree Mike" said:


The problem is :
What t parameter has related to the code you gave ?
(The only occurrence is its declaration : byval t as type).

Thanks :)

I don't understand your question unless you simply misread the statement
"if (t is gettype(Form)) then". You seemed to be wanting to create an
object x based on the type passed to the function as t. Your function
could be expanded to something like:

sub bla (byva t as type)
if (t is gettype(Form)) then
dim x as new Form
x.ShowDialog()
else if (t is gettype(Button)) then
dim x as new Button
this.Controls.Add(x)
else if (t is gettype(MySpecialControl)) then
dim x as new MySpecialControl
' do something specific to this control type...
end if
end sub
 
Sorry. It's my English.

I need the solution for the line you gave:
"else if (t is gettype(MySpecialControl)) then"
but ...
dim x as new MySpecialControl compiled,
but in code, I don't know t is MySpecialControl.
What I know t is a child of form (or a child of MySpecialControl) - that's
enough, and I want to make new instance of the child.

Something like :
If T Is GetType(MySpecialControl) Then
Dim x As New T.GetTypeOfChild() ... what should I write here ?
End If


Thanks :)
 
Am 24.04.2010 18:44, schrieb Mr. X.:
Sorry. It's my English.

I need the solution for the line you gave:
"else if (t is gettype(MySpecialControl)) then"
but ...
dim x as new MySpecialControl compiled,
but in code, I don't know t is MySpecialControl.
What I know t is a child of form (or a child of MySpecialControl) - that's
enough, and I want to make new instance of the child.

Something like :
If T Is GetType(MySpecialControl) Then
Dim x As New T.GetTypeOfChild() ... what should I write here ?
End If

"Child" means it is a derived class? If you know the type is Form, how do you know
the type of the child? Because there can be many classes derived from the Form class.

If you know the type in the calling method, why don't you create an instance in
the calling method? And where does the calling method know the type from?

In the end, what is your intention?
 
There is a derived "form" control of mine.
Any of the derived form has additional functionality (some methods ...
working with DB, etc ...)

There may be many forms of the derived form.
Each time I need to do : newForm.ShowDialog.
Only need to pass the type, and no need to create the form on the calling
program
(Each form has it's own elements, buttons, etc. and look different from
other forms).

Thanks :)
 
There is a derived "form" control of mine.
Any of the derived form has additional functionality (some methods ...
working with DB, etc ...)

There may be many forms of the derived form.
Each time I need to do : newForm.ShowDialog.
Only need to pass the type, and no need to create the form on the
calling program
(Each form has it's own elements, buttons, etc. and look different from
other forms).

Thanks :)

This may be what you are looking for:

Sub Bla(ByRef t As Type)
Dim f As System.Windows.Forms.Form = Activator.CreateInstance(t)
f.ShowDialog()
End Sub
Sub Main()
Bla(GetType(Form1))
End Sub

Main is calling Bla() with a type that is derived from Form, Called
Form1. Bla() creates an instance of a Form1 object and shows it.

Bla() does not need to know that it is a Form1, only that it can be
shown as it is derived from Form.

Is this what you wanted to do?
 
Am 24.04.2010 22:09, schrieb Family Tree Mike:
This may be what you are looking for:

Option Strict On? :)
Sub Bla(ByRef t As Type)
Dim f As System.Windows.Forms.Form = Activator.CreateInstance(t)
f.ShowDialog()
End Sub
Sub Main()
Bla(GetType(Form1))
End Sub

Main is calling Bla() with a type that is derived from Form, Called
Form1. Bla() creates an instance of a Form1 object and shows it.

Bla() does not need to know that it is a Form1, only that it can be
shown as it is derived from Form.

Is this what you wanted to do?

I'm curious too. If it is, I wonder what would by the purpose of Sub Bla because
it could be put in Sub Main:

dim f as form

f = new form1
f.showdialog

Even if it can be different Form classes it wouldn't make sense (til now):

dim f as form

select case whatever
case ThisOne
f=new form1
case ThatOne
f=new form2
end select

f = new form1
f.showdialog

With Sub Bla it's also a Select Case but instead it's GetType(Form1), GetType(Form2), etc.
So we will have to wait for Mr.X' answer. :-)
 
Mr. X,

A kind of variable type programming is tried endless times by persons who
want to use Managed code as a kind of VBA.

Managed code is designed to remove all that unpredictable in those at run
time interpreting code, to be stable. VB6 had it in the variant, and it is
now back in a way in C# in the Dynamic keyword (in fact a kind of Variant)
to be more available for scripting coders (or more concrete PHP coders).

So if you want to do things like this, then a language like PHP, VBA or
JavaScript is probably a much better choice for you.

In VB for Net we have this option already for years with the name of Option
Strict Of by passing a string, which than can act like a variant.

However, I'm not the one which will advice this Option which makes programs
less stable.
(If the string is not well format there is direct a break at user time)

I assume Armin does too.

:-)

Cor
 
Well, thanks.
Your design pattern example of using Activator, is exactly what I need too.

I have tried this for some other simpler controls, such as button.

Some new problems :
--------------------
1. I want to make a class that is inherited from a form,
and use it, add it to project, etc. at the same way I add new windows' form.
How can I do that?
2. Also, I need to exposed a property, but I should need a type.
When I do :
public property newForm() as Type
....

on the property I cannot choose a type,
but also I want that I can choose only types that are inherits the
MyNewForm.

How can I do that ?


Thanks :)
 
Am 25.04.2010 23:15, schrieb Mr. X.:
Well, thanks.
Your design pattern example of using Activator, is exactly what I need too.

Just curious: Why don't you use "New xyz" instead? I know, you have
different types, but now you have different GetType() statements.
Why is it better?

I have tried this for some other simpler controls, such as button.

Some new problems :

Either put it in a library and reference the library from any other project,
or save the Form as a template:

http://msdn.microsoft.com/en-us/library/6db0hwky(VS.90).aspx
2. Also, I need to exposed a property, but I should need a type.
When I do :
public property newForm() as Type
....

on the property I cannot choose a type,
but also I want that I can choose only types that are inherits the
MyNewForm.

How can I do that ?

Declare it "As MyNewForm".
 
Well, thanks.
Your design pattern example of using Activator, is exactly what I need too.

I have tried this for some other simpler controls, such as button.

Some new problems :
--------------------
1. I want to make a class that is inherited from a form,
and use it, add it to project, etc. at the same way I add new windows'
form.
How can I do that?

Here is a decent writeup on the topic. Look about half way through the
artical and see a section called "Exporting our class to a new template".

2. Also, I need to exposed a property, but I should need a type.
When I do :
public property newForm() as Type
...

on the property I cannot choose a type,
but also I want that I can choose only types that are inherits the
MyNewForm.

How can I do that ?


Thanks :)

Interesting. I've not tried that before, but I see where the property
is grayed out in the properties list. Aside from that, your question
about limiting the types is going to be a problem for a property. I'm
not sure how you could do that.

It sounds more and more like you could potentially use "generics". The
following class shows how to create a form using generics where the
parameter type is constrained to classes derived from type MyNewForm,
and that can be called to create a new instance.

Public Class MySpecialForm(Of T As {MyNewForm, New})
Inherits System.Windows.Forms.Form

Public Property NewForm As T
End Class


So now when you create an instance of this, you would do the following:

Dim x as new MySpecialForm(of Class1)

This assumes Class1 inherits from MyNewForm. You now can only set the
property x.NewForm to an object of type Class1 (or something derived
from class1). Within MySpecialForm, you can create an instance of the
sent in type (class1 in the example), as follows:

Sub foo()
Dim x As New T
x.ShowDialog()
End Sub
 
Mr. X. wrote:
1. I want to make a class that is inherited from a form,
and use it, add it to project, etc. at the same way I add new windows' form.
How can I do that?

In VB Express 2008 (didn't try in 2010, but it's probably the same)
you would have something like this:

a) Create the base form and customize it to your needs, placing the
controls that you want to appear in the descendant forms (the
descendant forms will also inherit the properties you set in the base
form as well).

b) Add a new class and inherit from your base form.

c) Build the application (from the build menu)

d) double click your new class name in the Solution Explorer to
customize it. The class will open in design mode, and you will see
that the controls that existed in the base for were automatically
added to the derived one (they even have some overlay marks indicating
that they are inherited).

e) rinse and repeat for any number of customized forms you want.
Notice that if you change anything in the base form(s), the changes
will only appear in the derived ones after you rebuild the
application.
2. Also, I need to exposed a property, but I should need a type.
When I do :
public property newForm() as Type
...

on the property I cannot choose a type,
but also I want that I can choose only types that are inherits the
MyNewForm.
How can I do that ?
<snip>

I'm not really sure of what you want here. Do you want a property that
will return a *new* object? Maybe this isn't the way properties are
supposed to work. A function would be more apropriate, I guess.

Best regards,

Branco.
 
Thanks - Work !!!
One little problem :
How can I create an objects that new has some parameters.
I have tried :
x = Activator.CreateInstance(MyControlType, controlArgs)
but this doesn't work.
Also
x = Activator.CreateInstance(MyControlType)(controlArgs)
doesn't work either.

MyControlType can be a form
controlArgs :
if I have constructor :
new(byRef t as dataTable).
....
dim x as MyControlType
....

Thanks :)
 
Back
Top