Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Question About Classes


  • Please log in to reply
5 replies to this topic
  • Guests
  • Last active:
  • Joined: --
I accidentally posted the following in Scripts. If a moderator could delete the one in Scripts, that'd be great. :).

I have a simple question. I've been using classes, such as CGUI and Crypt. I'm interested in making my own. However, this one area confuses me.

If I have a class inside of a class, how can I access values from the initial class? Take the following example:

class example{
	__new(){
		gui,1:+lastfound
		this.hwnd:=winexist()
		this.subclass:=new this.subclass()
	}
	class subclass{
		__new(){
			msgbox,% this.hwnd
			;Say I had some GDI+ crap to put in here... I'd need the HWND from the first class.
		}
	}
}

I know I could pass it as a parameter. For example, this.subclass:=new subclass(this.hwnd), but I'd rather not have duplicates. For example:
class example{
	__new(){
		gui,1:+lastfound
		this.hwnd:=winexist()
		this.subclass:=new this.subclass(this.hwnd)
	}
	class subclass{
		__new(h){
			this.hwnd:=h
			msgbox,% this.hwnd
		}
	}
}

Apologies if my requests sounds a bit... Nubbish. :p.



Thanks in advance!

  • Guests
  • Last active:
  • Joined: --
class c0 {
    class c1 {
        GetUpperClassName() {
            RegExMatch(this.__class, "\w+(?=\.\w+$)", upperClass)
            return upperClass
        }
    }
}

MsgBox % c0.c1.GetUpperClassName() "`n" c0.c1.__class
read more about prototypes.

  • Guests
  • Last active:
  • Joined: --
class c0 {
    class c1 {
        GetUpperClassName() {
            className := this.__class
            StringSplit, subClasses, className, .
            upperClass := subClasses0 - 1
            return subClasses%upperClass%
        }
    }
}

MsgBox % c0.c1.GetUpperClassName() "`n" c0.c1.__class
as you see, stringsplit won't accept this.__class as inputvar.

Wicked
  • Members
  • 504 posts
  • Last active: Nov 18 2018 02:17 AM
  • Joined: 07 Jun 2008
Does it now work in a __new()? Take the following:
class c0 {
	__new(){
		msgbox,% this.__class
		this.cn:=new c1()
		
	}
    class c1 {
		__new(){
			msgbox,% this.__class
		}
    }
}
Why is this.__class null in the second msgbox? I don't deal with classes often, I won't be much help in this area. Lol.

3nL8f.png


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
#NoEnv

Class C0 {

   __New(Value) {

      This.Value := Value

      MsgBox, 0, %A_ThisFunc%, % This.__Class

      This.SubClass := New This.C1([color=#BF0000]This[/color])



   }

   Class C1 {

      __New([color=#BF0000]Super[/color]) {

         MsgBox, 0, %A_ThisFunc%, % This.__Class . " - " . [color=#BF0000]Super[/color].Value

      }

   }

}

Instance := New C0("Hello world!")

ExitApp

? (I don't know whether ist has some unwanted side-effects!)

Prefer ahkscript.org for the time being.


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
When you do this.subclass:=new this.subclass(), there is no connection between this and the new object, therefore you will need to pass either the necessary properties (as in your second example), or a reference to the parent object (as in jm's example). There is also no connection from c1 (or instances of c1) to c0, and if you were to assign c0.c1 := "", there would be no connection either way.

Also, c1 is not a subclass, but a nested class. A subclass inherits properties and methods from another class.
class super {  [color=green]; Super-class (or base) of 'sub'[/color]
   ;...
}
class sub extends super {  [color=green]; Sub-class of 'super'[/color]
   ;...
}
class outer {
   class inner {  [color=green]; Nested class ('inner' is nested inside 'outer')[/color]
      ;...
   }
   class inner_sub extends outer {  [color=green]; Nested sub-class[/color]
      ;...
   }
   static inner2 := other  [color=green]; Functionally equivalent to nesting a class[/color]
}
class other {
   ;...
}

Why is this.__class null in the second msgbox?

The second MsgBox isn't even shown, because new c1() refers to an undefined local variable. new c0.c1 or new this.c1 would work, and would display "c0.c1".