3

I want to create a custom class object for an employee record in AutoHotkey. This class object would store an employee's age, name, and job title.

For example, in Java:

public class Employee {
    public int age;
    public String name;
    public String title;

    public Employee(int age, String name, String title) {
        this.age = age;
        this.name = name;
        this.title = title;
    }
}

I would then be able to set properties for new employees.

Employee worker1 = new Employee(22, "Timothy", "Programmer");
Employee worker2 = new Employee(26, "Anthony", "Quality Assurance");

I haven't been able to find anything with equivalent functionality in AHK. From what I can tell, objects in AHK are fairly basic in functionality.

How do I create a class object with custom properties in AutoHotkey?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • 1
    https://autohotkey.com/docs/Objects.htm – puhlen Aug 24 '17 at 20:07
  • 2
    [The documentation for objects](https://autohotkey.com/docs/Objects.htm#Usage_Objects) seems to imply that objects properties can be given properties without needing to declare them beforehand. (i.e. `Employee.Name := "Timothy"`) – Stevoisiak Aug 28 '17 at 15:30
  • I found a guide which explains AHK's classes from the basics: **[Classes in AHK, Basic tutorial.](https://autohotkey.com/boards/viewtopic.php?t=6033)** – Stevoisiak Oct 04 '17 at 16:19

1 Answers1

8

I don't use classes in AutoHotkey, but it should be like so:

Class Employee{
    __New(age, name, title)
    {
        this.age := age
        this.name := name
        this.title := title
    }
}

worker1 := new Employee(22, "Timothy", "Programmer")
worker2 := new Employee(26, "Anthony", "Quality Assurance")
David Metcalfe
  • 2,237
  • 1
  • 31
  • 44
  • 2
    It's really an obscure construction to accomplish this, thanks for filling the gaps in the user guides. –  May 08 '20 at 12:34