Home All Groups Group Topic Archive Search About

send the class type as a parameter.

Author
24 Apr 2010 10:01 AM
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 :)

Author
24 Apr 2010 11:02 AM
Armin Zingler
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))





--
Armin
Author
24 Apr 2010 12:28 PM
Mr. X.
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 :)

Show quoteHide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:eLMM3254KHA.980@TK2MSFTNGP04.phx.gbl...
> 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))
>
>
>
>
>
> --
> Armin
Author
24 Apr 2010 12:33 PM
Mr. X.
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 :)

Show quoteHide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:eLMM3254KHA.980@TK2MSFTNGP04.phx.gbl...
> 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))
>
>
>
>
>
> --
> Armin
Author
24 Apr 2010 12:44 PM
Family Tree Mike
On 4/24/2010 8:33 AM, Mr. X. wrote:
Show quoteHide quote
> 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 :)
>
> "Armin Zingler" <az.nospam@freenet.de> wrote in message
> news:eLMM3254KHA.980@TK2MSFTNGP04.phx.gbl...
>> 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))
>>
>>
>>
>>
>>
>> --
>> Armin
>

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

--
Mike
Author
24 Apr 2010 3:04 PM
Mr. X.
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
>
> --
> Mike

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

Thanks :)
Author
24 Apr 2010 3:58 PM
Family Tree Mike
On 4/24/2010 11:04 AM, Mr. X. wrote:
Show quoteHide quote
> 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
>>
>> --
>> Mike
>
> 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

--
Mike
Author
24 Apr 2010 4:44 PM
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


Thanks :)


Show quoteHide quote
"Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message
news:#mOQ#b84KHA.4520@TK2MSFTNGP02.phx.gbl...
> On 4/24/2010 11:04 AM, Mr. X. wrote:
>> 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
>>>
>>> --
>>> Mike
>>
>> 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
>
> --
> Mike
Author
24 Apr 2010 5:06 PM
Armin Zingler
Am 24.04.2010 18:44, schrieb Mr. X.:
Show quoteHide quote
> 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?


--
Armin
Author
24 Apr 2010 7:55 PM
Mr. X.
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 :)

Show quoteHide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:eB5irE94KHA.6132@TK2MSFTNGP05.phx.gbl...
> 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?
>
>
> --
> Armin
Author
24 Apr 2010 8:09 PM
Family Tree Mike
On 4/24/2010 3:55 PM, Mr. X. wrote:
> 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?

--
Mike
Author
24 Apr 2010 10:18 PM
Armin Zingler
Am 24.04.2010 22:09, schrieb Family Tree Mike:
Show quoteHide quote
> On 4/24/2010 3:55 PM, Mr. X. wrote:
>> 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:

Option Strict On?  :)

Show quoteHide quote
>      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. :-)

--
Armin
Author
24 Apr 2010 11:34 PM
Family Tree Mike
On 4/24/2010 6:18 PM, Armin Zingler wrote:
Show quoteHide quote
> Am 24.04.2010 22:09, schrieb Family Tree Mike:
>> On 4/24/2010 3:55 PM, Mr. X. wrote:
>>> 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:
>
> Option Strict On?  :)

Unfortunately, I'm not sure _yet_ if Mr. X can do what he wants with
Option Strict On...

--
Mike
Author
25 Apr 2010 9:15 PM
Mr. X.
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 :)
Author
26 Apr 2010 1:20 AM
Family Tree Mike
On 4/25/2010 5:15 PM, Mr. X. wrote:
> 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

--
Mike
Author
26 Apr 2010 1:32 AM
Family Tree Mike
On 4/25/2010 9:20 PM, Family Tree Mike wrote:
Show quoteHide quote
> On 4/25/2010 5:15 PM, Mr. X. wrote:
>> 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".
>

Sorry, forgot the link:
http://www.vbdotnetheaven.com/UploadFile/progalex/AdsCreateExportClasses01242006114527AM/AdsCreateExportClasses.aspx

--
Mike
Author
27 Apr 2010 1:47 PM
Branco Medeiros
Mr. X. wrote:
<snip>
> 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.
Author
29 Apr 2010 9:44 PM
Mr. X.
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 :)
Author
30 Apr 2010 9:56 AM
Mr. X.
Solved.
What I did :
A constructor on the inherited class.

I should check out using template to insert the code behind.

Thanks :)

Show quoteHide quote
"Mr. X." <nospam@nospam_please.com> wrote in message
news:ejOXyU#5KHA.3656@TK2MSFTNGP06.phx.gbl...
> 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 :)
Author
25 Apr 2010 9:16 AM
Cor Ligthert[MVP]
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


Show quoteHide quote
"Mr. X." <nospam@nospam_please.com> wrote in message
news:#fHFqU54KHA.3184@TK2MSFTNGP05.phx.gbl...
> 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 :)
>