Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

Use a variable for Class Name in Classic ASP

I have several classes [ClassA, ClassB, ClassC].  I want to create an instance of one of the classes but the name of the class is in a variable [myClass].  I am wondering how I would use the variable to make any instance of one of my classes.

For example, I would like to do something like this:
Set objClass = New myClass

Open in new window


Can this be done?
0
typetoit
Asked:
typetoit
  • 2
2 Solutions
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
i'm a bit unclear on what you're asking but set instantiate a class and set it to a reference (or variable), is exactly how you have it.

here's a good read on OOP Programming in ASP
0
 
typetoitAuthor Commented:
I want to instantiate a class where the name of the class is stored in a variable.  
For example, myClass could be ClassA or ClassB.  

Dim myClass, objClass

myClass = "ClassA"
Set objClass  = New myClass

In the above example, I want to instantiate ClassA using myClass.  Will this work?
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
i don't believe you can do that, I imagine you would get an error.

You can easily prove whether or not it'll work, set up a test page with that code, then try to access one of the classes properties...
0
 
Ryan ChongCommented:
you probably can't create it directly but you can use a Function to return the class object based on the value you passing in, try something like this:

<%

Dim myClass, objClass

myClass = "ClassA"
Set objClass  =  getClass(myClass)
objClass.SetA1(12345)
response.write "<br>objClass.getA1 = " & objClass.getA1()

myClass = "ClassC"
Set objClass2  =  getClass(myClass)
objClass2.SetC2("Hello World! " & Now())
response.write "<br>objClass2.getC2 = " & objClass2.getC2()


Function getClass(v)
    Select Case v
    Case "ClassA"
        Set getClass = new ClassA
    Case "ClassB"
        Set getClass = new ClassB
    Case "ClassC"
        Set getClass = new ClassC
    Case else
        Set getClass = new ClassA
    End Select
End Function


Class ClassA
	Private v_A1
	Private v_A2

	Private Sub Classv_Initialize()
		
	End Sub

	Private Sub Classv_Terminate()
		
	End Sub

    Public Function setA1(v)
        v_A1 = v
    End Function

    Public Function setA2(v)
        v_A2 = v
    End Function

    Public Function getA1()
        getA1 = v_A1
    End Function

    Public Function getA2()
        getA2 = v_A2
    End Function
End Class

Class ClassB
	Private v_B1
	Private v_B2

	Private Sub Classv_Initialize()
		
	End Sub

	Private Sub Classv_Terminate()
		
	End Sub

    Public Function setB1(v)
        v_B1 = v
    End Function

    Public Function setB2(v)
        v_B2 = v
    End Function

    Public Function getB1()
        getB1 = v_B1
    End Function

    Public Function getB2()
        getB2 = v_B2
    End Function
End Class

Class ClassC
	Private v_C1
	Private v_C2

	Private Sub Classv_Initialize()
		
	End Sub

	Private Sub Classv_Terminate()
		
	End Sub

    Public Function setC1(v)
        v_C1 = v
    End Function

    Public Function setC2(v)
        v_C2 = v
    End Function

    Public Function getC1()
        getC1 = v_C1
    End Function

    Public Function getC2()
        getC2 = v_C2
    End Function
End Class

%>

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now