Solved

Use a variable for Class Name in Classic ASP

Posted on 2016-09-20
4
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 33

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 33

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 51

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Select record with the most recent date 14 71
Cannot open file from ASP if user is "logged in" 13 32
ASP get fieds value 2 21
Ajax on ASP 2 36
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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