Sign in to follow this  
Followers 0
SkinnyWhiteGuy

Object-Like Programming

9 posts in this topic

I've been playing with this for awhile. I've seen people ask for Object Oriented syntax in AutoIt, and be told it isn't going in, at least not in version 3 code. Well, after reading how C++ got it's start, and just to see if I could, I decided to try to make an "Object-Like" set of functions, to give people a little Object programming. So, here is what I made.

It's not much, and it's actually pretty simple. I use 2 Dictionary Objects to store the member variables, and the internal names of functions. I used these for 2 reasons: 1) they were easy, and 2) they allowed me to pass things through the Call() function by reference, which is how I give the objects their own functions (kind-of). Since COM objects are actually just references to the actual objects, you can pass those through Call, and the called function will be operating on the original data, which works out just right for this.

As a side effect of this, though, you can get an instant reference to any object just by setting a variable to equal the already made object. To get an actual copy, I made a Clone function, which does just what you'd expect: creates a new object, copies all relevant data, and returns that new object reference.

Included in the zip is the Classes.au3 file that does all this, plus 3 files to show what it can do. ClassDog and ClassCollie are just example classes that show how you can use this to make semi base classes and then child classes from the base class from before. Dog_Test shows these classes in use.

Note, this is severly lacking in many areas that a true object-oriented system would have, such as destructors for objects, protection of variables, and many other things. In making this library, though, I came to realize that, while Object-Oriented programming is good, it's not really that different from Procedural programming. It's more a different way of thinking about the problem, and a different method of solving it. All the deficiencies that these functions suffer from can be watched and done correctly by hand, as long as the programmer using them is diligent enough to do so (like, for example, not messing with internal variables that are meant to be private).

I would love to see what any of you thought of this. It really isn't all that great, and can be done almost the same with regular UDFs. I just did it like this to get me used to thinking in Object terms, and like I said earlier, I do like a challenge.

Classes.zip

Share this post


Link to post
Share on other sites



Interesting idea, I will take a look at it tonight.

Share this post


Link to post
Share on other sites

hi,

I can make object like this:

;=========================================================

; Create the struct

; struct {

; int var1;

; unsigned char var2;

; unsigned int var3;

; char var4[128];

; }

;=========================================================

$str = "int var1;ubyte var2;uint var3;char var4[128]"

$a = DllStructCreate($str)

if @error Then

MsgBox(0,"","Error in DllStructCreate " & @error);

exit

endif

;=========================================================

; Set data in the struct

; struct.var1 = -1;

; struct.var2 = 255;

; struct.var3 = INT_MAX; -1 will be typecasted to (unsigned int)

; strcpy(struct.var4,"Hello");

; struct.var4[0] = 'h';

;=========================================================

DllStructSetData($a,"var1",-1)

DllStructSetData($a,"var2",255)

DllStructSetData($a,"var3",-1)

DllStructSetData($a,"var4","Hello")

DllStructSetData($a,"var4",Asc("h"),1)

But this is one object, how do i make array of object and using it ? we can't declare: Dim $array[50]

Share this post


Link to post
Share on other sites

pctester2020, that isn't an Object, that is a Struct. Structs store all data needed in one variable, but have no related functions with them. They are not objects, though you could use them in the same manner I used COM objects and make them pseudo-objects. If you want help with these, try in General Help.

Share this post


Link to post
Share on other sites

pctester2020, that isn't an Object, that is a Struct. Structs store all data needed in one variable, but have no related functions with them. They are not objects, though you could use them in the same manner I used COM objects and make them pseudo-objects. If you want help with these, try in General Help.

definitely cool.

I've been working with VBScript to acheive this in au3. After September 28 I'll post in the example scripts.

IVAN

Share this post


Link to post
Share on other sites

Very fascinating.! This will ease the pain of missing some OO capabilities.

Share this post


Link to post
Share on other sites

Not bad at all. But I don't expect a massive shift to everyone using this yet. ;)

True. Building a whole app with Classes UDF sounds a little bit scary, though. It feels.. umm.. clunky maybe.

But the idea is still tempting..

Are there any drawbacks when using the Scripting.Dictionary object there?

Share this post


Link to post
Share on other sites

None that I could tell, it seems to work just fine. As far as clunky, yeah, unless Jon and the other Devs add support for OOP into the language itself, clunky is as close as you can get (unless you went the whole route of writing a converter that took a madeup AutoItOOP language, and converted everything to regular AutoIt Code).

Share this post


Link to post
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
Sign in to follow this  
Followers 0