We’re working on a web project where we had planned to display containers dynamically in a web page. The project will end up with hundreds of these individual web containers and we had hoped to manage them via a dynamic index or dictionary indicating the sequence and allowing us to include/exclude specific containers.
The problem is none of these containers is instantiated when the application is first launched, and due to the number of containers, I’m thinking it would be inadvisable to do so.
I feel like we’re missing something, but in searching for a solution I came across this forum post which does seem to imply that there’s not a simple solution to the problem.
Is it possible to instantiate a class/container at runtime without hard coding, using a case statement as referred to the referenced post, or some other rather clunky solution? I was hoping there was some means of finding/grabbing the class/container by name, but I’m afraid if we never instantiate it it’s not available at run time…
If a class is not used / referenced in code somewhere somehow it gets stripped out
So it won’t exist at run time for you to be able to do something like
dim o as Object = new ContainerByName( some string holding the name )
What you MIGHT do is something like a factory
function CreateNewContainerByName( s as string ) as WebContainer
select case s
return new container1
return new container2
// to indicate that someone messed up and
// asked you to create a container you didn't add to this factory
raise new myCustomException
now because there is a “new Container” line for each container you might create they are “used” so they won’t get stripped out and you can call this when ever you need
We talked about using a case statement, I just wanted to make sure we weren’t missing something here…
At compile time you don’t have any magic way of going through a project & finding all the containers & saying “please don’t strip this one out”
There are some techniques you can do to make it so things won’t get stripped out BUT at runtime you still can’t walk through all possible classes that have not been stripped out unless there is an instance. The runtime knows about all instances but not about “every possible class that could be created” - so you WOULD need to create an instance to know of all the ones you can create.
And there are maybe some techniques that you could use to make it so you always create an instance of everything but then you’ve created them just to be able to find them & then maybe throw them away.
The straight forward way is to have one simple method like above
Yes, we also talked about instantiating everything on App Open or something like that, but were concerned about the overhead because of the sheer number of classes/containers we have in this project.
I prefer the ‘factory’ idea where we just instantiate things as needed.