Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
|
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 12th, 2004, 05:52 PM
Registered User
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dpinkerton Send a message via Yahoo to dpinkerton
Default Reflection to convert UserControl Dynamically

Hello - I am new at this posting bit. :)

I am trying to Load a user control at runtime - no problem to do this as ....

Dim analystView As AnalysisView = CType(Page.LoadControl("..\..\UserControls\Analysi sView.ascx"), AnalysisView)

The problem is that I am trying to dynamically determine what user control to load - we have several that might be loaded. Rather than coding a lot of conditional logic I am trying to convert the user control to the dynamic type using reflection.

This is my latest attempt, but it gives me a compile error:

Dim moduleArray() As [Module]
Dim ctrlType As Type
Dim myModule As [Module] = moduleArray(0)

ctrlType = myModule.GetType(ctrlClassName)
ctrl = CType(Page.LoadControl(ctrlLocation), ctrlType)


The compile error is: Type 'ctrlType' is not defined - for the red ctrlType reference above.

I've run the reflection code by commenting out the conversion and I am successfully finding the right class in the module so the problem does not lie with the reflection - but with the conversion (of course since it won't even compile that way. )

I guess VB does not like me using CType with a variable for the Type parameter - does anyone know how I can cast the UserControl to a variable value at run time?

Thanks!!

dpinkerton
 
Old March 13th, 2004, 05:41 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I am not sure if you can do what you're trying to accomplish.

Since you have to know in advance what kind of control to expect (if you need generic control functionality you could cast it to a control instead), why not use a switch / Select Case statement instead? You can use the type name to determine the type, and in each Case block cast to the appropriate control.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 15th, 2004, 04:55 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What are you trying to accomplish in the end? Can you explain what you need to do with the dynamically loaded control AFTER you have loaded it?

If you just need to add a user control to some controls collection on a page then just load it and add it:

<somecontrol>.Controls.Add(LoadControl("whatever.a scx"))

Or you can get it as a Control object, because all controls come from the Control class:

Dim objControl As Control = LoadControl("whatever.ascx")
<somecontrol>.Controls.Add(objControl)

Unless you need to access specific members of the dynamic control, you shouldn't need to do much more than just load it and add it to a Controls collection somewhere.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old March 18th, 2004, 11:45 AM
Registered User
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dpinkerton Send a message via Yahoo to dpinkerton
Default

Thank you both for your responses. I believe I cannot do this - as you both thought. What I did instead was created a base user control that all my other controls (of this type) inherit from . this way I can cast to the base class and still set the properties I need to.

It's not that I couldn't do it with Select logic, but with four of five controls that are similar, it seemed like a sloppy way to code it. The base class was needed anyway, but I still think I should be able to cast to a type determined at run time, not compile time, but VB at least doesn't like that. :) No biggie, now it's all nice and neat.

Thanks again for your responses!

dpinkerton

 
Old March 18th, 2004, 05:17 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The other thing you could do if you have a specific set of properties that you need to get access to is to implement an interface on each of the controls. Then, instead of casting the control to a specific control type, you just cast to the interface and use the interface's members.

However, I think your solution of using a user control base class is more elegant and probably cleaner in the end, as you would need to have the same public members on the control if you used an interface solution.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Reflection-TargetInvocationException dipak_2111 C# 2005 0 January 11th, 2007 01:08 AM
Reflection jayakumar.cj ASP.NET 1.0 and 1.1 Basics 1 July 15th, 2006 06:12 AM
Reflection and WebControl jdang67 ASP.NET 2.0 Professional 1 October 20th, 2005 04:14 PM
Reflection Events CrazyLegsCooper VS.NET 2002/2003 4 September 8th, 2005 10:06 PM
Reflection ffbalota C# 2 January 8th, 2004 03:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.