call a function inside a class Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gerard
Posts: 72
Joined: 01 Apr 2020, 13:14
Location: Buenos Aires
Contact:

call a function inside a class

26 Sep 2020, 13:41

Big regards to all.
I find myself studying a little php to be able to maintain a web page. There is something that I have been working on that I would very much like to be able to do with AHK. I understand that they are different languages, but I can't find a better explanation of what I want to do.
How can I pass parameters and execute a function that I have inside a class
En php

Code: Select all

<?php
class Microondas {
function __construct($color, $potencia) {
$this->color = $color;
$this->potencia = $potencia;
 }

function informacion() {
print("Un microondas de color: ".$this->color.", con potencia ".$this->potencia."<br><br>");
 }
  }

$m = new Microondas("verde", 10);
$m->informacion();
?>
A poorly written example of what I want to be able to understand the concept of class

Code: Select all

Class Speak
 {
Str := "Null"
Hablar(Str)
{
This.Str := Str
MsgBox % Str
 }
 }
 
 f3::
 Texto := New Speak("Probando el uso de las clases")
 % Texto.Hablar()
 Return
How would this be well written?
Thanks in advance
If my tarzan english is not understood, let's blame google translate
teadrinker
Posts: 4373
Joined: 29 Mar 2015, 09:41
Contact:

Re: call a function inside a class

26 Sep 2020, 18:51

Code: Select all

Class Speak
{
   __New(str := "") {
      this.str := str
   }
   Hablar() {
      MsgBox % this.str
   }
}
 
F3::
   Texto := New Speak("Probando el uso de las clases")
   Texto.Hablar()
Return
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: call a function inside a class  Topic is solved

26 Sep 2020, 19:07

Hola @gerard,

Code: Select all

% Texto.Hablar()
Here, you must remove the trailing single %: it as a special meaning only in command input parameters - to force an expression:

Code: Select all

Texto.Hablar() ; by default evaluated as an expression in this case by ahk
MsgBox % Texto.Hablar() ; forces the expression in a parameter that does not directly support it
Morevover, you call your method without parameter - yet in the definition of your method, the Str parameter is not optional: the method call silently fails.

The same occurs here:

Code: Select all

Texto := New Speak("Probando el uso de las clases")
...not exactly the same since this time it is question of a superflous parameter; in this case, ahk simply ignores superfluous parameters; at least the call does not fails:

Code: Select all

Texto := New Speak("Probando el uso de las clases")
MsgBox % IsObject(Texto) ; actually shows '1' (true)
 MsgBox % Texto.__Class ; actually shows 'Speak'
The fact remains, however, that, nowhere you define your constructor and its first parameter it should be supplied with, as you done with you hablar method.
Actually, your class lacks its (custom) constructor; in ahk __construct becomes __New, know as a meta-function.
The method must be define in the base object (in plain text in the body of your class or programatically using something like Microondas.__New := Func("myConstructor")).

Spoiler


Cheers

A_AhkUser
my scripts
gerard
Posts: 72
Joined: 01 Apr 2020, 13:14
Location: Buenos Aires
Contact:

Re: call a function inside a class

26 Sep 2020, 20:11

Thank you very much to both.
A_AhkUser. The explanation is very clear. Thank you very much for that.
If my tarzan english is not understood, let's blame google translate

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder, Sem552, teadrinker and 199 guests