Jump to content

Simple, OOP-like syntax in AutoIt


Recommended Posts

I'm wondering if it's possible to set up data types in AutoIt that are a little more complex than arrays, but not really as complex as full-blown objects either. I know there's an OOP UDF floating around, but I'm not exactly sure how it works.

Basically, if I've got a $person array that's set up like this:

$person[0] = 'Jane' ;first name
$person[1] = 'Doe' ;last name
$person[2] = 23 ;age
$person[3] = 'F' ;gender
; and so on ...

return $person

I'd like to be able to set this up as something like:

$person.firstname

$person.lastname

$person.age

....

The reason I ask is because I've got a script using an array with 15+ elements, and it gets a bit difficult to try and remember what is $array[11] when I see it in code. Plus, if I ever need to change the order of the elements, that is a huge pain in the arse when it's being passed around from function to function.

Link to comment
Share on other sites

  • Moderators

WorknMan,

I recognse the problem! :>

An Associative Array might be the thing you are looking for - my favourite implementation is this one by Nutster.

Take a look and see if it is what you want - please come back and ask again if you have any questions on how to use it or it does not fit your requirements. :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

If the elements are going to be static you could always set them to common names.

#include <array.au3>

Global $person[4]

$first = 0
$last = 1
$age = 2
$sex = 3

$person[$first] = 'Jane' ;first name
$person[$last] = 'Doe' ;last name
$person[$age] = 23 ;age
$person[$sex] = 'F' ;gender
; and so on ...

_arraydisplay ($person)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I'm wondering if it's possible to set up data types in AutoIt that are a little more complex than arrays, but not really as complex as full-blown objects either. I know there's an OOP UDF floating around, but I'm not exactly sure how it works.

For example this how:

#include "oLinkedList.au3"

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

_AutoItObject_Startup()



Global $oMyList = LinkedList()
$oMyList.add(_CreateItem("Jane", "Doe", 23, "F"))
$oMyList.add(_CreateItem("Jack", "TheRipper", 38, "M"))
$oMyList.add(_CreateItem("Jill", "Scott", 40))
$oMyList.add(_CreateItem("Joey", "Tribiani", 30, "M"))

Global $sFemales, $sMales

For $oMember In $oMyList
    If $oMember.gender = "F" Then $sFemales &= @TAB & $oMember.Name & "    " & $oMember.LastName & "    age " & $oMember.Age & @CRLF
    If $oMember.gender = "M" Then $sMales &= @TAB & $oMember.Name & "    " & $oMember.LastName & "    age " & $oMember.Age & @CRLF
Next


ConsoleWrite(">Females:" & @CRLF & $sFemales)
ConsoleWrite(">Males:" & @CRLF & $sMales)



Func _CreateItem($sName, $sLastName, $iAge, $sGender = "F")
    Local $oClass = _AutoItObject_Class()

    $oClass.AddProperty("Name", $ELSCOPE_PUBLIC, $sName)
    $oClass.AddProperty("LastName", $ELSCOPE_PUBLIC, $sLastName)
    $oClass.AddProperty("Age", $ELSCOPE_PUBLIC, $iAge)
    $oClass.AddProperty("Gender", $ELSCOPE_PUBLIC, $sGender)

    Return $oClass.Object
EndFunc

When you look closer you will see it's a real life saver.

Btw, there can be number of variations on the subject.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Ok, I got a lot of info to sift through here, but I have a question for trancexx, or anyone who knows :unsure:

I see this line in your code:

Global $oMyList = LinkedList()

What is that LinkedList() function?

Edited by WorknMan
Link to comment
Share on other sites

LinkedList is a UDF wich is defined in oLinkedList.au3. It creates an object which represents a linked list.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

I like to use enumerations. So I would have something like:

Global Enum $eFNAME, $eLNAME, $eAGE, $eSEX, $eNUM_TRAITS

Global $Person[$eNUM_TRAITS]

$Person[$eFNAME] = "John"

$Person[$eLNAME] = "Doe"

$Person[$eAGE] = 21

$Person[$eSEX] = "M"

Link to comment
Share on other sites

LinkedList is a UDF wich is defined in oLinkedList.au3. It creates an object which represents a linked list.

Well, I kind of figured as such, but was wondering where to get the UDF :unsure: I thought it might be a part of the AutoItObject UDF, but don't see any reference to it in the documentation or the code.

Link to comment
Share on other sites

Well, I kind of figured as such, but was wondering where to get the UDF :unsure: I thought it might be a part of the AutoItObject UDF, but don't see any reference to it in the documentation or the code.

Sorry for not saying... you can find oLinkedList.au3 inside AutoItObject package in \Examples folder.

AutoItObject package.

Just place it together with AutoItObject.au3 into the script's folder.

Edited by trancexx

♡♡♡

.

eMyvnE

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...