Solved

Use a variable for Class Name in Classic ASP

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to hide hopscotch tour popup page reload? 2 63
Update table value with join syntax 2 61
Forcing form refresh - classic ASP 7 59
IP API - need data... 4 15
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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now