So working with classes is still pretty new to me and I'm relatively inexperienced.
Is there a way to define a constant within a class, such that, for example, in any given instance of class Cube, this.NumberOfSides always equals 6?
Creating Constants Within a Class? Topic is solved
Re: Creating Constants Within a Class? Topic is solved
Unsure if we have actual constants, but static does the trick.
Code: Select all
class MyClass
{
static my_const := 5
MyMethod()
{
return this.my_const
}
}
one := new MyClass()
two := new MyClass()
MsgBox % one.my_const "`n" two.my_const "`n" one.MyMethod()
Use
Code: Select all
[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
Re: Creating Constants Within a Class?
Code: Select all
class Cube {
sides[] {
get {
return 6
}
set {
throw Exception("Attempted modification of const")
}
}
}
testCube := new Cube()
MsgBox, % testcube.sides ;result 6
testCube.sides := 3 ;exception
testCube2 := new Cube()
MsgBox, % testcube2.sides ;result 6
testCube2.sides := 5 ;exception
Re: Creating Constants Within a Class?
I mean an immutable number. In my specific use case, I need to store the decimal value of an RGBA value, so that instead of needing to remember 4294967295, I can just reference this.colorWhite, and the same with three other colors I use with that class.
Who is online
Users browsing this forum: Google [Bot] and 378 guests