Solved

Use a variable for Class Name in Classic ASP

Posted on 2016-09-20
4
74 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 52

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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