How pass type in as parameter?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

I want to something as simple as:

UserControl uctTemp;

But the type will be passed in to the function (which will be an existing
user control like "uctMyUserControl1")

So, how can I pass in the string "uctMyUserControl1" and then somehow dim an
instance of it?

uctMyUserControl1 uctTemp;

Thanks,
Ron
 
Ronald,

Are you sure you want to pass the name of the control to your function,
or the name of the type of the control? If you pass the name of the type of
the control (or a Type instance itself), then you can use reflection to get
the Type (if you pass a string) and then construct an instance of that type
using reflection.

If you pass simply a field name, you could use reflection, but you need
to know the class on which the field is declared as well as the instance
holding it in order to get the instance that you are referring to.

All in all, it's easier to just pass the type to your method, and
construct an instance from there.

Hope this helps.
 
Can you do something like

private void someFunction (object userControl, string userControlType)
{
//code here
}
 
Yup, I was able to figure it out that way.. thanks for the reply.


Nicholas Paldino said:
Ronald,

Are you sure you want to pass the name of the control to your function,
or the name of the type of the control? If you pass the name of the type
of the control (or a Type instance itself), then you can use reflection to
get the Type (if you pass a string) and then construct an instance of that
type using reflection.

If you pass simply a field name, you could use reflection, but you need
to know the class on which the field is declared as well as the instance
holding it in order to get the instance that you are referring to.

All in all, it's easier to just pass the type to your method, and
construct an instance from there.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
I want to something as simple as:

UserControl uctTemp;

But the type will be passed in to the function (which will be an existing
user control like "uctMyUserControl1")

So, how can I pass in the string "uctMyUserControl1" and then somehow dim
an instance of it?

uctMyUserControl1 uctTemp;

Thanks,
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top