#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Question Vbscript class inheritance


    Hi all,

    Iíve been working on the following class inheritance technique to help put some life in to some legacy apps I'm maintaining written in vbscript.

    http://www.aspmessageboard.com/showthread.php?t=229924

    This is seriously simple yet very powerful, proper inheritance and polymorphism, constructor parameters etc, and from my quick tests there is no performance penalty between this technique and native classes.

    I thought I'd post it up in case it is of any use to anybody in the same boat as myself

    If you need any help with it or you have any suggestions please let me know.

    Thanks,

    Reiss

    Code:
    <%
    class ClassHelper
    	private dict 
    
    	public default function addMethod(name)
    		set dict(name) = getRef(dict("type") & name)
    	end function
    	
    	private function createDict()
    		set createDict = server.CreateObject("scripting.dictionary")
    	end function
    	
    	private function copyDict(super,child)
    		dim key
    		
    		for each key in super
    			if isObject(super(key)) then
    				set child(key) = super(key)
    			else
    				child(key) = super(key)
    			end if
    		next
    		
    		set copyDict = child
    	end function
    	
    	public function newClass(name)
    		set dict = createDict()
    		dict("type") = name
    		set newClass = dict
    	end function
    	
    	public function extends(super,childType)
    		set dict = copyDict(super,createDict())
    		set dict("parent") = super
    		dict("type") = childType		
    		set extends = dict
    	end function
    	
    	public function [new](super,arrParams)	
    		dim dict : set dict = copyDict(super,createDict())
    		call dict("initialize")(dict,arrParams)
    		set [new] = dict
    	end function
    end class
    
    dim klass : set klass = new ClassHelper
    
    'START NEW CLASS
    	dim Fighter : set Fighter = klass.newClass("Fighter")
    
    	'PROPERTIES
    	Fighter("name") = "unkown"
    	Fighter("power") = 100
    	Fighter("speed") = 100
    	
    	'METHODS
    	function FighterInitialize(this,arrParams)
    		this("name") = arrParams(0)
    	end function : klass "initialize"
    	
    	function FighterSwingSword()
    		FighterSwingSword = "slash slash"
    	end function : klass "swingSword"
    	
    	function FighterIncreasePower(this,power)
    		dim thisPower
    		thisPower = this("power")
    		thisPower = thisPower + power
    		this("power") = thisPower 
    	end function : klass "increasePower"
    'END CLASS
    
    'START NEW CLASS
    	dim Ninja : set Ninja = klass.extends(Fighter,"Ninja")
    	
    	'PROPERTIES
    	Ninja("stealth") = 100
    	Ninja("speed") = 80
    	
    	'METHODS
    	function NinjaInitialize(this,arrParams)
    		this("stealth") = arrParams(0)
    		this("name") = arrParams(1)
    	end function : klass "initialize"
    	
    	function NinjaSwingSword()
    		NinjaSwingSword = "woosh woosh"
    	end function : klass "swingSword"
    'END CLASS
    
    'START NEW CLASS
    	dim DarkNinja : set DarkNinja = klass.extends(Ninja,"DarkNinja")
    'END CLASS
    
    'USE CLASSES
    dim fighterJohn : set fighterJohn = klass.new(Fighter,array("john"))
    dim ninjaBob : set ninjaBob = klass.new(Ninja,array(70,"bob"))
    dim darkNinjaBill : set darkNinjaBill = klass.new(DarkNinja,array(20,"bill"))
    
    response.write Fighter("type") & "<br />"
    response.write ninjaBob("type") & " is a child of " & ninjaBob("parent")("type") & "<br />"
    response.write darkNinjaBill("type") & " is a child of " & darkNinjaBill("parent")("type") & " who is a child of " & darkNinjaBill("parent")("parent")("type") & "<br />"
    response.write "<br />"
    
    response.write "i'm " & fighterJohn("name") & ", who are you? <br />"
    response.write "i'm " & ninjaBob("name") & " and i'm a " & ninjaBob("type") & "<br />"
    response.write "well i'm a true " & fighterJohn("type") & " and i hate ninjas! take this " & fighterJohn("swingSword")()  & "<br />"
    response.write "I have a stealth rating of " & ninjaBob("stealth") & " and a speed rating of " & ninjaBob("speed") & "! you'll never hit me... now take this " & ninjaBob("swingSword")() & " " & ninjaBob("parent")("swingSword")() & "<br />"
    response.write "You missed! as i have a speed rating of: " & fighterJohn("speed") & " ha ha ha <br />"
    response.write "Wait! " & fighterJohn("name") & ", you are my father! I may be a " & ninjaBob("type") & ", but I am a descendant of " & ninjaBob("parent")("type") & "<br />"
    response.write "Well I am your grandson " & fighterJohn("name") & "! I have a power rating of " & darkNinjaBill("power") & " but as my father " & ninjaBob("name") & " is here I can increase my power to " & darkNinjaBill("increasePower")(darkNinjaBill,200) & darkNinjaBill("power")
    %>
    Last edited by atwork8; October 16th, 2008 at 03:46 PM. Reason: Now easier to add a method to a class
  2. #2
  3. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Jul 2005
    Location
    Oxford UK
    Posts
    3,897
    Rep Power
    993
    Thanks so much for sharing with us.
    Nothing is Impossible bcoz IMPOSSIBLE itself says..
    I M POSSIBLE........................
    Be cool !!!!!!!!
  4. #3
  5. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    thanks for sharing, I've moved this thread to the Code Bank.

Similar Threads

  1. Oracle Helper Class with .Net
    By ceema in forum .NET Development
    Replies: 0
    Last Post: March 8th, 2007, 12:00 AM
  2. Replies: 2
    Last Post: September 8th, 2006, 11:16 AM
  3. Class Event triggering a method on a Form...
    By Tryst in forum .NET Development
    Replies: 1
    Last Post: May 23rd, 2006, 07:54 AM
  4. Dependent Drop Down Boxes in Classic ASP
    By Misty in forum ASP Development
    Replies: 12
    Last Post: September 21st, 2005, 01:27 AM
  5. Interview questions on ASP/VB/.NET (PLEASE) URGENT
    By Shashi in forum .NET Development
    Replies: 9
    Last Post: December 30th, 2003, 01:10 PM

IMN logo majestic logo threadwatch logo seochat tools logo