Hello everybody,I spend a couple of days trying to understand how Classes in AHK works.Thanks to a forum community I manage to get basic representation of it.

Unfortunately in Help file Classes described in very complicated way without examples. So I decided to make basic tutorial for beginners. Looks like it is big Tutorial and I could make mistakes so if you find them pleas let me know and I'll fix them.

So what is Class Variables? Imagine you car has name for example BMW - this is property and it looks like - CarName:=BMW. Also it has 4 wheels it is also property. For example WheelsAmount:=4

So what is Class Method? Imagine when turn the steering wheel the car turns - this is method, and it looks like - TurnSteerWheel().Also when you use key to open car this is also method for example CarOpenKey().

So imagine you made plane where all that Variables and Methods was defined, now you want to build a car. This is how it looks in AHK:

;First we have to declare class:Class Car{ CarName:="BMW" WheelsAmount:=4 TurnSteerWheel(){ Some Commands here...} CarOpenKey(){ Some Commands here...}}; Then create Instane of that class with name BMWConceptBMWConcept:=New Car

This was very basics, so lets make something with Classes in AHK.Lets make simple Class of Window. Window has a lot of properties like Windows Title, Windows ID, Position, Size, e.t.c. these will be Class Variables.Also we can make with window a lot of actions for example Activate it, Get its Title, Get its ID, Get it's position e.t.c. Actions like this will be our Class Methods.So lets make simple Class for Notepad of one Variable and one Method.

1. Was added methot - Get(). This method retrieves Window Title and Window ID from Active window.2. Variable title was removed, because we will use method Get() to get Title.2. Method Activate was modified and now it use not Window Title but Window ID. 3. Method AnnounceWinProps() was added, that show us Title and ID that was retrieved by Get()4. Hotkey ^!a was modified now it activates Notepad, than run Get() Method.5. Hotkey ^!s was added. This hotkey activates Window with Method Activate() if ID is True(was retrieved).

Now lets add nested Class to our Class Window with couple of variables. So what is Nested Class?Nested Class is just Class inside another Class and you can access it like this BaseClass.NestedClass To acces variable from Nested class use this: BaseClass.NestedClass.Var or Method - BaseClass.NestedClass.Method().

As you can see A is declared like simple variable and this kind of variable has name - Instance Variable. Instance variables assigns to instance only when they was created. We do bot create Instance from Window.Tmp, but if we decide to do it it will look like this: SomeWin1:= New Window.Tmp. After we created instance this Variable A will be available here: SomeWin1.ALet's move to variable B, it has word "Static" this kind of variables calls - Static Variables. This variables belong to Class itself, so that is why we could access it, and our message showed us that B:=1.

Now let's talk about Class Inheritance. So what is it?Imagine you have Class A and it has Var1 and Var2. You created Class B and you want this Class to derive everything from A. Another words you want Class B have the same variables and Methods that has Class A. Let's make it:

As you can see we add New Class Notepad that extends Window Class and create Instance - SomeNot.Also take a look at Hotkey - ^!a. As you can see Instanse SomeNote use all inherited methods from Class Window.

And last...Let's imagine that we want some actions occurs when we create Instance. To initialize that actions we will use: __New, this is how it look like:

As you can see __New was added in Notepad Class. In this section When we create Instance SomeNot it execute method Run() and Get() now we do not need to run them from hotkey, look at hotkey ^!a::, we do not need those action any more. Also now we can use ^!s to activate Notepad.

Also there is __Del like __New it works when Instance deletes but now I'm not sure how it works, I'll back to it later.

So lets make final touch, lets create Class Calc that will inherit Class Window also we will add to it method Run()

samardac wrote:So what is class? Imagine that you want to build a Car. To make it you have to have Plan of it. This plane is Class. In AHK it looks like this:...So what is Instance?Imagine that you build Car using Plan, and this Car is Instance.So Plan is Class and Car is Instance. In AHK creating Instance from Class looks like this:...Every Class has Variables and Methods.

It's good to see someone creating some basic tutorials on objects. Honestly, I didn't examine the entire post, but it appears your teaching is targeted at the noobie crowd.I wanted to make sure it is understood that a class is not only a plan (or blueprint) for an Instance, but the class is an object itself. See Keyword Prototype instead of Class for more discussion.

jethrow, Yep it's for noobie, I found that in Help File explanation is complicated, but people have to start from something.

Class is not only a plan (or blueprint) for an Instance, but the class is an object itself

Thanx for thise remark. I preffer that beginers think about Classes as blueprint for better understanding at the begining. When they becom familiar with classes they can follow your link for fother studing.

Also, in your first code example, ClassName is a valid class that doesn't have any Variables nor Methods.