Jump to content


Photo

Function Obj<--->Class<--->File ?


  • Please log in to reply
5 replies to this topic

#1 nothing

nothing
  • Members
  • 127 posts

Posted 07 June 2012 - 12:55 PM

Hi,
I'm Looking for a function that can convert a class to a object and reverse.
i.e
Class ClassA
{
....
}
ObjA := ClassToObj(ClassA)
ClassB := ClassFromObj(ObjA)
B := new ClassB
Or a function that can save/load a class to/from A File.
Class ClassA
{
....
}
A := new ClassA
ClassNameToFile(ClassA,"ClassA.CLN")
ClassToFile(A,"A.Cla")

ClassB := ClassNameFromFile("ClassA.CLN")
B := new ClassB
C := ClassFromFile("A.Cla")
Thanks for any help

#2 sinkfaze

sinkfaze
  • Moderators
  • 6089 posts

Posted 07 June 2012 - 01:01 PM

I'm Looking for a function that can convert a class to a object and reverse.


:?:

A class is an object.

Custom Objects":3d8qs0y9">

...AutoHotkey emulates classes by translating class definitions into ordinary objects.



#3 nothing

nothing
  • Members
  • 127 posts

Posted 07 June 2012 - 01:09 PM

yes, it's a "class" object, but how to convert "normal" object to "class" object which has keyword "this" .
And,the main point in my questions is Class<--->File.

#4 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 07 June 2012 - 10:11 PM

An object doesn't have the keyword this. A method has the keyword this. Semantically speaking, there is no way to define a method outside of a class definition. You would need to define a function with the this parameter explicitly, at the start of the parameter list, and then use that function as a method.

Each method has a hidden parameter named this, which typically contains a reference to an object derived from the class. However, it could contain a reference to the class itself or a derived class, depending on how the method was called. Methods are stored by reference in the class object.
Source: Objects

class A {
    M() {
        MsgBox % "M called via " this.__Class
    }
}

B := {}
B.M := A.M
B.__Class := "B"

C := {}
C.M := Func("C_M")
C.__Class := "C"

C_M(this) {
    MsgBox % "M called via " this.__Class
}

_a := new A
_a.M()
_b := new B
_b.M()
_c := new C
_c.M()
Note: This is covered under Objects - Prototypes.

Or a function that can save/load a class to/from A File.

AutoHotkey_L doesn't support that. AutoHotkey.dll might.

#5 nothing

nothing
  • Members
  • 127 posts

Posted 08 June 2012 - 01:06 AM

thanks a lot, Lexikos!

#6 HotKeyIt

HotKeyIt
  • Fellows
  • 6134 posts

Posted 08 June 2012 - 07:00 AM

In AutoHotkey_H(dll) you can load any script from file/text:
exe:=AhkExported() ;http://www.autohotkey.com/community/viewtopic.php?t=60630

exe.addScript("

(

Class myclass {

  static var:=""value1""

  variable:=""value2""

  method(){

    MsgBox `% this.var ""``n"" this.variable

  }

}

)")

my:=new myclass

my.method()