Jump to content


Photo

[AHK_L] Simple Class Design


  • Please log in to reply
No replies to this topic

#1 IsNull

IsNull
  • Fellows
  • 990 posts

Posted 23 November 2010 - 07:05 AM

This Topic is not really about the sense of the code provided below, but about the syntax sugar - its plain AHK_L Code.

Gosub, SetUpObjectBase
Progress, m2 b zh0, Collectiing dlls...
dlls := CollectDLLs()

for k,dll in dlls {
	Progress, m2 b zh0, % "registring " dll.FileName()
	dll.Register()
}
MsgBox % "Registered " dlls.Count() " dlls!"

exitapp

CollectDLLs(){
global
	dlls := New(Collection)
	loop, %a_scriptdir%\*.dll
		dlls.Insert(Dll(A_LoopFileFullPath))
	return dlls
}


SetUpObjectBase:

Collection := Object()
{
	Collection.Count := "Collection_Count"

	Collection_Count(this){
		i := 0
		for e in this
			i++
		return i
	}
	
	Collection(){ 
		global Collection
		return New(Collection)
	} 
}

Dll := Object()
{
	Dll.Register := "Dll_Register"
	Dll.FileName := "Dll_FileName"
	Dll.Path := ""
	
	Dll_Register(this){
		run, % "regsvr32 /s""" this.Path """"
	}
	Dll_FileName(this){
		fullPath := this.Path
		SplitPath, fullPath, fileName
		return fileName
	}
	Dll(path){ ; Constructor
		global 
		dll := New(Dll)
		dll.Path := path
		return dll
	}
}

return

New(obj){
	return, Object("base", obj)
}



I like the way to write a "class" in this maner:

Dll := Object()
{
	Dll.Register := "Dll_Register"
	Dll.FileName := "Dll_FileName"
	Dll.Path := ""
	
	Dll_Register(this){
		run, % "regsvr32 /s""" this.Path """"
	}
	Dll_FileName(this){
		fullPath := this.Path
		SplitPath, fullPath, fileName
		return fileName
	}
	Dll(path){ ; Constructor
		global 
		dll := New(Dll)
		dll.Path := path
		return dll
	}
}
This gives a clean code Block for a class and also a way to define a constructor and write more straightforward code :)

Any other ideas in this direction (besides fincs nice precompiler)?