Understanding Classes

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Understanding Classes

26 Sep 2018, 12:15

Hello,
I have read the material and have created a class of my own (along with adding comments along the way), and I wanted to make sure that I was understanding everything correctly when it comes to classes.

Code: Select all

/* This class demonstates some of the basics for a class which includes the following:
	- The __new method
	- A function that returns a search
	- The set and get methods
	- A variable that can be changed
*/
class Vehicle {
	;|Creates a new set of parameters and asks for an input of 3 variables, if any/all are blank will replace them with the string Null
	__new(make := "Null", model := "Null", horsepower := "Null") {
		this.make := (make = "") ? "Null" : make					;|Contains an error check, if field is blank will make the new variable Null
		this.model := (model = "") ? "Null" : model 				;|Contains an error check, if field is blank will make the new variable Null
		this.horsepower := (horsepower = "") ? "Null" : horsepower	;|Contains an error check, if field is blank will make the new variable Null
		this.speed := 0												;|Sets the default speed of the vehicle (can be changed later)
		this.vehicleInformation := Object("Make", this.make, "Model", this.model, "Horsepower", this.horsepower)
		;|The above creates an object/array from the given variables which are used later for searches and to make a json reponse
	}
	;|Returns either the searched information or the whole array as a json reponse
	information(Field := "") {
		obj := "{"
		count = 0								;|This count is used if a search parameter is made and to stop if the count is reached
		If (Field = "") {						;|This will create the whole obj variable json reponse
			for key, value in this.vehicleInformation {
				If (value.Count() > 0) {		;|If the value is an object/array we want to expand that array
					obj .= """" key """: {"		;|Adds the main key before hand to follow the json format
					for index, subvalue in value
						obj .= """" . index . """: " . ((subvalue ~= "^[-+]?\d+(\.\d+)?$") ? ("") : ("""")) . subvalue . ((subvalue ~= "^[-+]?\d+(\.\d+)?$") ? ("") : ("""")) . ((value.Count() = A_Index) ? "" : ", ") . ""
					obj.= "}, "
				} Else							;|If the value is not an object
					obj .= """" . key . """: " "" .  """" value """" . ((this.vehicleInformation.Count() = A_Index) ? "" : ", ") . ""
			}
		} Else If (Field.Count() = "") {		;|Check if 1 or multipule variables where given
			for key, value in this.vehicleInformation {
				If (Field = key) {				;|Check if the Field search matches the key and if true only produce that part of the total reponse
					If (value.Count() > 0){		;|If the value is an object/array we want to expand that array
						obj .= """" key """: {"
						for index, subvalue in value
							obj .= """" . index . """: " . ((subvalue ~= "^[-+]?\d+(\.\d+)?$") ? ("") : ("""")) . subvalue . ((subvalue ~= "^[-+]?\d+(\.\d+)?$") ? ("") : ("""")) . ((value.Count() = A_Index) ? "" : ", ") . ""
						obj.= "}, "
					} Else {					;|If the value is not an object
						count++
						obj .= """" . key . """: " "" .  """" value """" . ((this.vehicleInformation.Count() = count) ? "" : ", ") . ""
					}
				}
			}
		} Else {								;|If parameter is not empty and contains multipule variables then we want to split the Field into search values
			for i, search in Field {			;|Splits the Field into several search values that can be extrapolated
				for key, value in this.vehicleInformation {
					If (search = key) {			;|Notice that the variable search is used instead of Field as Field will return nothing since its an array
						If (value.Count() > 0){	;|If the value is an object/array we want to expand that array
							obj .= """" key """: {"
							for index, subvalue in value
								obj .= """" . index . """: " . ((subvalue ~= "^[-+]?\d+(\.\d+)?$") ? ("") : ("""")) . subvalue . ((subvalue ~= "^[-+]?\d+(\.\d+)?$") ? ("") : ("""")) . ((value.Count() = A_Index) ? "" : ", ") . ""
							obj.= "}, "
						} Else {				;|If the value is not an object
							count++
							obj .= """" . key . """: " "" .  """" value """" . ((this.vehicleInformation.Count() = count) ? "" : ", ") . ""
						}
					}
				}
			}
		}
		obj := RTrim(obj, ", ")
		obj .= "}"
		return obj
	}
	;|changes the speed of the vehicle, note the error check to make sure the value is not blank
	speed {
/* 	Note that with this.speed is defined as a variable, so setting it in this way makes it a variable which we can change
	If we want more changing variables can be made like so
		make {
			set {
				return this.m := value  ;|-note that you should make a new varible for the value to be placed in
			} get {
				return this.m
			}
		}
	This will allow us to change the make variable to whatever we want it too
	Also notice that value is the value we are putting in the this.m variable and that we are not making the variable equal to this.make
	- Set will set the new variable
	- Get will get the variable for this.speed, so if you call that variable anywhere in the class you can get the latest variable */
		set {					;|This will set the variable this.s as this.speed is already used and since we want the variable to change we will use set
			If (value = "") {	;|Check to make sure the variable is not blank
				MsgBox % "Cannot be blank"
				return
			}
			Else
				return this.s := value
		} get {					;|This will get the variable whenever this.speed is used in the class
			If (this.s = "") {	;|Check to make sure the variable is not blank
				throw "Cannot be blank"
				return
			}
			Else
				return this.s
		}
	}
	;|obtains the current speed of the vehicle, if an error occurs then an error message will be given
	speedGet() {
		try
			MsgBox % this.speed
		catch e
			MsgBox % e
	}
}
;|If a number string is given in an array (shown below) and does not contain a decimal point then a litteral number will occur instead of a string
obj := new Vehicle("Nissan", "Titan", Object("LSX", 138, "XD", 200))	;|Create 1 new vehicle with and object for item 3 (horsepower in this case)
obj2 := new Vehicle("Nissan", "Frontier", "")							;|Create 1 new vehicle where the third variable is blank (horsepower in this case)
MsgBox % obj2.information()												;|Returns the full json reponse for the second vehicle we made
MsgBox % Clipboard := obj.information()									;|Returns the full json reponse for the first vehicle we made
MsgBox % obj.information(["Model", "Horsepower"])						;|Here we only want the Model and Horsepower from the first vehicle (which will also return the array for the horsepower
																		;|also note that return is not in Alphabetical order, but instead in the order we gave the search
obj.speed += -54		;|Changing the speed to negative
obj.speed += 50			;|Adding 50 to the speed
obj.speedGet()			;|Returns the current speed and defaults to 0 as 0 was defined as the default speed in the __new part of the class
Note that it complains about a { not existing, but its just a length thing with comments (no idea why).

One other question I have is when its more appropriate to use a class instead of a function as I know that function are used when a similar statement is needed multiple times, but instead of writing it again and again you can use a function to do the exact same thing. With classes from what I understand is that you create a new instance at the beginning and are able to call different parts of the class. Is it similar to multiple functions being under the same thing or am I a little off on this. Thanks.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, gsxr1300, inseption86, jaka1, mebelantikjaya, mikeyww and 302 guests