views:

406

answers:

2

Anyone know how I can dynamically load a control inside of a shared/static function? The function itself is inside of a mustinherit/abstract class. (It's an ASP.NET project in VB) I want to do something like this:
VB:

    Public Shared Function GetWidget(ByVal name As WidgetName) As Control
        Select Case name
            Case WidgetName.Name1
                Return LoadControl("~/Control1.ascx")
            Case WidgetName.Name2
                Return LoadControl("~/Control2.ascx")
            Case WidgetName.Name3
                Return LoadControl("~/Control3.ascx")
        End Select
    End Function

my C# is a little rusty, so this might have some syntax errors:

Public Static Control GetWidget(WidgetName name)  
{  
    switch (name)  
    {  
        Case WidgetName.Name1:  
            return LoadControl("~/Control1.ascx");  
            break;  
        Case WidgetName.Name2:  
            return LoadControl("~/Control2.ascx");  
            break;  
        Case WidgetName.Name3:  
            return LoadControl("~/Control3.ascx");  
            break;  
    }  
}  

(Where WidgetName is an enumerator.)

I'm getting "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.", but I don't understand this error. I understand what it means, I just don't understand why calling LoadControl isn't seen by the compiler as being an explicit instance of the class. What's not explicit about using LoadControl to create a new control from a file? I tried creating a new user control and initializing it, then setting it to a different control with LoadControl to no avail. I also don't want to do a DirectCast because I'm trying to put this in a shared, mustinheret (abstract) class, which therefore doesn't have an .aspx file to write in a <%@ Reference Control="~/SomeControlPath.ascx" %>, so the class name is unavailable.

What I'm trying to do is write a static function that takes some value and returns a control based only on that control's source file location. The end result is a user-modifiable list of controls. They get a column of controls that they freely add, remove, or reorder based on a static list of available child controls that I specify. I'm not married to this approach; it might be really wrong in more ways than one.

yeah, I know the static strings being there is code smell, it doesn't actually look like that; it's a simplification for the sake of asking the question.

C#, VB, or plain English explanations all welcome.

A: 
Rippo
holy shit. I need to take a break. The first solution is totally obvious and works just fine, I'm going way too nuts over something so trivial.
jorelli
It is not about what is getting returned...it is that you cannot call the LoadControl method from inside the static method...because there is no instance.
CSharpAtl
No worries, been there! Grab a coffee and stay off the beer :) :)
Rippo
@CSharpAtl - You are correct which is why I suggested an alternative.
Rippo
@Rippo...your casting solution will not work...because you cannot call `LoadControl()` from inside the static method.
CSharpAtl
haha I didn't drink last night, I've just been going at it for a few hours non-stop on a very finicky project I inherited, so I'm a little frazzled ;)
jorelli
+1  A: 

LoadControl is an instance method on the TemplateControl class which the Page class inherits from, and you do not have an instance of the Page class inside your static method (there is no this object because it is a static method).

CSharpAtl
ah, thank you for the technicals, that's helpful to know. I understand the error now. Up until now I hadn't really thought much about the context of the LoadControl definition, I just took it for granted and used it.
jorelli
In your intellisense menu...`LoadControl` should not have come up as an option in your static method..
CSharpAtl
yeah, it doesn't. I wrote the body of the method with the signature specifying it as an instance method, then decided it would be an easier read if it was shared so I changed the signature, which is when I started to see the error and didn't quite understand it.
jorelli