Jump to content

AutoItObject - How to use .Create() ?


 Share

Recommended Posts

I am using

I have created an object and I would like other object to inherit the first one. Here is the code:

#include "AutoitObject.au3"
Opt("MustDeclareVars", 1)

; Error monitoring
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")

; Initialize AutoItObject
_AutoItObject_StartUp()

; Create Object
Global $oObject = _SomeObject()
Global $oClone = _cloneObject()

; Set some property
$oObject.Title = "Hey"

;Display messagebox
$oObject.MsgBox("Test")

$oClone.Title = "Cloned"
$oClone.MsgBox("Clone object")

; Relese
$oObject = 0 ; <-!Comment this out to see the new behavior!

; That's it! Goodby



; Define object
Func _SomeObject()
    Local $oClassObject = _AutoItObject_Class()
    $oClassObject.Create()
    $oClassObject.AddMethod("MsgBox", "_Obj_MsgBox")
    $oClassObject.AddProperty("Title", $ELSCOPE_PUBLIC, "Something")
    $oClassObject.AddProperty("Text", $ELSCOPE_PUBLIC, "Some text")
    $oClassObject.AddProperty("Flag", $ELSCOPE_PUBLIC, 64 + 262144)
    $oClassObject.AddDestructor("_DestructorForSomeObject")
    Return $oClassObject.Object
EndFunc   ;==>_SomeObject

Func _Obj_MsgBox($oSelf, $string)
    MsgBox($oSelf.Flag, $oSelf.Title, $string)
EndFunc   ;==>_Obj_MsgBox

Func _DestructorForSomeObject($oSelf)
    ConsoleWrite("!...destructing..." & @CRLF)
EndFunc

Func _ErrFunc()
    ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc   ;==>_ErrFunc

Func _cloneObject()
    Local $oClass = _AutoItObject_Class()

    With $oClass
        .Create($oObject)
    EndWith

    Return $oClass.Object
EndFunc

For some reason $oClone doesnt inherit $oObject. Code returns "! COM Error ! Number: 0x8002000E ScriptLine: 59 - Invalid number of parameters." (Line 59 = .Create($oObject))

even though in help file it says that $oParent parameter can be given for .Create() method. => "Create([$oParent = 0]) - creates AutoItObject object"

I know I can use _AutoItObject_Create($oObject) and that one works fine, but why doesnt .Create($oObject) ?

Am I doing something wrong?

Im using AutoIt version v3.3.8.0 and

AutoItObject UDF version v1.2.8.2.

Link to comment
Share on other sites

Use _AutoItObject_AddMethod() and ...AddProperty() in the class which inherits from another class.

For instance, if you have a class which defines some methods and you want another class to inherit those methods. So, class_one() defines a method and class_two() wants to use that method then put Local $this = class_one()) inside of class_two().

#AutoIt3Wrapper_AU3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6 -w 7 -d

#include "AutoitObject.au3"

_AutoItObject_StartUp()

Global $concrete = concrete_class()

$concrete.HelloWorld()

$concrete.HelloWorldAgain()

$concrete = ''

_AutoItObject_Shutdown()

; -------------------------------------------------------------------------------------------------

Func abstract_class()
    Local $this = _AutoItObject_Class()
    $this.AddMethod("HelloWorld", "abstract_hello_world")
    $this.AddDestructor("abstract_dtor")
    Return $this.Object
EndFunc

Func abstract_dtor(Const $this)
    MsgBox(0, "Abstract", "Destructed")
    Return $this
EndFunc

Func abstract_hello_world(Const $this)
    MsgBox(0, "Abstract", "Hello World")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------

Func concrete_class()
    Local $this = abstract_class()
    _AutoItObject_AddMethod($this, "HelloWorldAgain", "concrete_hello_world_again")
    _AutoItObject_AddDestructor($this, "concrete_dtor")
    Return $this
EndFunc

Func concrete_dtor(Const $this)
    MsgBox(0, "Concrete", "Destructed")
    Return $this
EndFunc

Func concrete_hello_world_again(Const $this)
    MsgBox(0, "Concrete", "Hello World Again")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------

Heavily edited due to giving totally wrong answer first few times around.

Edited by LaCastiglione
Link to comment
Share on other sites

Use _AutoItObject_AddMethod() and ...AddProperty() in the class which inherits from another class.

For instance, if you have a class which defines some methods and you want another class to inherit those methods. So, class_one() defines a method and class_two() wants to use that method then put Local $this = class_one()) inside of class_two().

Indeed, that is working and you can also do it like this:

#AutoIt3Wrapper_AU3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6 -w 7 -d

#include "AutoitObject.au3"

_AutoItObject_StartUp()

Global $abstract = abstract_class()
Global $concrete = concrete_class()

$concrete.HelloWorld()

$concrete.HelloWorldAgain()

$concrete = ''

_AutoItObject_Shutdown()

; -------------------------------------------------------------------------------------------------

Func abstract_class()
    Local $this = _AutoItObject_Class()
    $this.AddMethod("HelloWorld", "abstract_hello_world")
    $this.AddDestructor("abstract_dtor")
    Return $this.Object
EndFunc

Func abstract_dtor(Const $this)
    MsgBox(0, "Abstract", "Destructed")
    Return $this
EndFunc

Func abstract_hello_world(Const $this)
    MsgBox(0, "Abstract", "Hello World")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------

Func concrete_class()
    Local $this = _AutoItObject_Create($abstract)
    _AutoItObject_AddMethod($this, "HelloWorldAgain", "concrete_hello_world_again")
    _AutoItObject_AddDestructor($this, "concrete_dtor")
    Return $this
EndFunc

Func concrete_dtor(Const $this)
    MsgBox(0, "Concrete", "Destructed")
    Return $this
EndFunc

Func concrete_hello_world_again(Const $this)
    MsgBox(0, "Concrete", "Hello World Again")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------

But now the question remains - why cant it be done like this:

#AutoIt3Wrapper_AU3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6 -w 7 -d

#include "AutoitObject.au3"

_AutoItObject_StartUp()

Global $abstract = abstract_class()
Global $concrete = concrete_class()

$concrete.HelloWorld()

$concrete.HelloWorldAgain()

$concrete = ''

_AutoItObject_Shutdown()

; -------------------------------------------------------------------------------------------------

Func abstract_class()
    Local $this = _AutoItObject_Class()
    $this.AddMethod("HelloWorld", "abstract_hello_world")
    $this.AddDestructor("abstract_dtor")
    Return $this.Object
EndFunc

Func abstract_dtor(Const $this)
    MsgBox(0, "Abstract", "Destructed")
    Return $this
EndFunc

Func abstract_hello_world(Const $this)
    MsgBox(0, "Abstract", "Hello World")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------

Func concrete_class()
    Local $this = _AutoItObject_Class()
    $this.Create($abstract)
    $this.AddMethod($this, "HelloWorldAgain", "concrete_hello_world_again")
    $this.AddDestructor($this, "concrete_dtor")
    Return $this.Object
EndFunc

Func concrete_dtor(Const $this)
    MsgBox(0, "Concrete", "Destructed")
    Return $this
EndFunc

Func concrete_hello_world_again(Const $this)
    MsgBox(0, "Concrete", "Hello World Again")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------

According to the help file both ways presented above should be correct. Only problem is that you cant use any parameters with "$this.create()" method.

Does above scripts work on your computer?

[EDIT] Some syntax errors in code.

Edited by Eemuli
Link to comment
Share on other sites

I would file a bug report over at the AIO project page.

Indeed, that is working and you can also do it like this:

Indeed. This does work. However, for me it calls the dtor for abstract twice.

According to the help file both ways presented above should be correct. Only problem is that you cant use any parameters with "$this.create()" method. Does above scripts work on your computer?

Just fix up your syntax if you decide to use your second example...

#AutoIt3Wrapper_AU3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6 -w 7 -d

#include "AutoitObject.au3"

_AutoItObject_StartUp()

Global $abstract = abstract_class()
Global $concrete = concrete_class()

$concrete.HelloWorld()

$concrete.HelloWorldAgain()

$concrete = ''

_AutoItObject_Shutdown()

; -------------------------------------------------------------------------------------------------

Func abstract_class()
    Local $this = _AutoItObject_Class()
    $this.AddMethod("HelloWorld", "abstract_hello_world")
    $this.AddDestructor("abstract_dtor")
    Return $this.Object
EndFunc

Func abstract_dtor(Const $this)
    MsgBox(0, "Abstract", "Destructed")
    Return $this
EndFunc

Func abstract_hello_world(Const $this)
    MsgBox(0, "Abstract", "Hello World")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------

Func concrete_class()
    Local $this = _AutoItObject_Class()
    $this.Create($abstract)
    $this.AddMethod("HelloWorldAgain", "concrete_hello_world_again")
    $this.AddDestructor("concrete_dtor")
    Return $this.Object
EndFunc

Func concrete_dtor(Const $this)
    MsgBox(0, "Concrete", "Destructed")
    Return $this
EndFunc

Func concrete_hello_world_again(Const $this)
    MsgBox(0, "Concrete", "Hello World Again")
    Return $this
EndFunc

; -------------------------------------------------------------------------------------------------
Edited by LaCastiglione
Link to comment
Share on other sites

  • 1 month later...

Hi,

I have the same problem on Win Xp,

i have show your post on http://autoitobject.origo.ethz.ch/issues

So for me, the way is to redefine the "_AutoItObject_Class()" fonction

I have write a customized _AutoItObject_Class with oParent parameter :

;================================================================================================
; Customized Class Object
;=====================================================================================================
Func _AutoitObj_Class($oParent)
Local $oThis = _AutoItObject_Create()
_AutoItObject_AddMethod($oThis, "AddProperty", "_Object_AddProperty")
_AutoItObject_AddMethod($oThis, "AddMethod", "_Object_AddMethod")
_AutoItObject_AddMethod($oThis, "Create", "_Object_Create")
_AutoItObject_AddProperty($oThis, "Object", $ELSCOPE_PUBLIC, 0)
_AutoItObject_AddProperty($oThis, "Parent", $ELSCOPE_PUBLIC, $oParent)
Return $oThis
EndFunc

and the customized fonction :

Func _Object_Create($oSelf)
$oSelf.Object = _AutoItObject_Create($oSelf.Parent)
EndFunc
Func _Object_AddMethod($oSelf, $Name, $Fonction)

Local $oObject = $oSelf.Object
_AutoItObject_AddMethod($oObject, $Name, $Fonction)
EndFunc
Func _Object_AddProperty($oSelf, $Name, $Scope, $Value)
Local $oObject = $oSelf.Object
_AutoItObject_AddProperty($oObject, $Name, $Scope, $Value)
EndFunc

For use it you just have to :

Global $oMyObject = _MyObject()
$oMyObject.MyMethod

Func _MyObject($oParent=0)
Local $oClass = _AutoitObj_Class($oParent)
$oClass.Create
$oClass.AddProperty("MyProperty", $ELSCOPE_PUBLIC, 4)
$oClass.AddMethod("MyMethod", "testMyMethod")
Return $oClass.Object
EndFunc
Func testMyMethod($oSelf)
MsgBox(0, "", "MyProperty = "&$oSelf.MyProperty)
EndFunc

FOR ME IT WORK with Parameter $oParent

If you have suggestion for the code please post it

Link to comment
Share on other sites

Sorry,

i correct some mistake

So for me, the way is to redefine the "_AutoItObject_Class()" fonction

I have write a customized _AutoItObject_Class with oParent parameter :

;================================================================================================
; Customized Class Object
;=====================================================================================================
Func _AutoitObj_Class($oParent)
Local $oCustom = _AutoItObject_Create()
      _AutoItObject_AddMethod($oCustom, "AddProperty", "_Object_AddProperty")
      _AutoItObject_AddMethod($oCustom, "AddMethod", "_Object_AddMethod")
      _AutoItObject_AddMethod($oCustom, "Create", "_Object_Create")
      _AutoItObject_AddProperty($oCustom, "Object", $ELSCOPE_PUBLIC, 0)
      _AutoItObject_AddProperty($oCustom, "Parent", $ELSCOPE_PUBLIC, $oParent)
Return $oCustom
EndFunc

and the customized fonction :

;=================================================================================
; Customized Fonction Create - Class Object
;=======================================================================================Func _Object_Create($oSelf)
     $oSelf.Object = _AutoItObject_Create($oSelf.Parent)
EndFunc

;=======================================================================================
; Customized Fonction AddMethod - Class Object
;=======================================================================================
Func _Object_AddMethod($oSelf, $Name, $Fonction)
     Local $oObject = $oSelf.Object
     _AutoItObject_AddMethod($oObject, $Name, $Fonction)
EndFunc


;======================================================================================
; Customized Fonction AddProperty - Class Object
;=======================================================================================
Func _Object_AddProperty($oSelf, $Name, $Scope, $Value)
     Local $oObject = $oSelf.Object
     _AutoItObject_AddProperty($oObject, $Name, $Scope, $Value)
EndFunc

Example For use it you just have to :

#include <AutoItObject.au3>
Global $oParent
_AutoItObject_Startup()

;// Obj 1
Global $oMyObject1 = _AutoItObject_Create()
_AutoItObject_AddProperty($oMyObject1, "MyProperty_Obj1", $ELSCOPE_PUBLIC, "Object 1")

;// Obj 2 with parent Obj 1
Global $oMyObject2 = _MyObject($oMyObject1)
$oMyObject2.MyMethod

;// Class definition Obj 2
Func _MyObject($oParent=0)
Local $oClass = _AutoitObj_Class($oParent)
$oClass.Create
$oClass.AddProperty("MyProperty_Obj2", $ELSCOPE_PUBLIC, "Object 2")
$oClass.AddMethod("MyMethod", "testMyMethod")
$oCustom = 0 ; delete the custom object
Return $oClass.Object
EndFunc

;// Methode obj 2
Func testMyMethod($oSelf)
MsgBox(0, "", "MyProperty_Obj1 = "&$oSelf.MyProperty_Obj1 &@CRLF& _
"MyProperty_Obj2 = "&$oSelf.MyProperty_Obj2 )
EndFunc

FOR ME IT WORK with Parameter $oParent

If you have suggestion for the code please post it

Edited by M2C
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...