Solved

Use a variable for Class Name in Classic ASP

Posted on 2016-09-20
4
36 Views
Last Modified: 2016-09-23
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
Comment
Question by:typetoit
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Big Monty
ID: 41807176
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
 
LVL 1

Author Comment

by:typetoit
ID: 41807275
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
 
LVL 32

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
ID: 41807328
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
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 250 total points
ID: 41812083
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now