467,074 Members | 842 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

How pass type in as parameter?

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
Mar 20 '07 #1
  • viewed: 1821
Share:
3 Replies
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]
- mv*@spam.guard.caspershouse.com

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:uG*************@TK2MSFTNGP04.phx.gbl...
>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


Mar 20 '07 #2
Can you do something like

private void someFunction (object userControl, string userControlType)
{
//code here
}

"Ronald S. Cook" wrote:
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
Mar 20 '07 #3
Yup, I was able to figure it out that way.. thanks for the reply.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eg****************@TK2MSFTNGP06.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:uG*************@TK2MSFTNGP04.phx.gbl...
>>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



Mar 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dion Heskett | last post: by
9 posts views Thread by Jay Douglas | last post: by
3 posts views Thread by Brett | last post: by
5 posts views Thread by David++ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.