Getter ready, get Setter, Go!

I am loving the stupid names I’ve been using for these posts. Pretty soon you’ll have no idea what I am talking about.

In my last post, I showed you have to create a new class and the topic of access modifiers was mentioned. I would like to expand on that topic and use it to introduce a new one for class creation.

Access Modifiers

Access modifiers are used when defining variable or functions within a class. They essentially dictate what other classes have access to them. Is AS3 we have 4 types of access modifiers:

public – any class can access

private – only the owning class can access

protected – the owner and subclasses of the owner can access

internal – classes in the same package can access

You will be dealing with the first two public and private on a regular basis when writing classes. Lets get into an example.

Below is a very simple class that has a String variable and a method that outputs the value of the string variable,

package{

import flash.display.Sprite;

public class MyClass extends Sprite{

public var myName:String = “not set”;

public function MyClass(){

//constructor

}

public function outputName():void{

trace(myName);

}

}

}

Okay now lets create an instance of that class and change the value of name by accessing it directly using dot syntax

import MyClass;

var newClass:MyClass = new MyClass();

newClass.myName = “Ryan”;

newClass.outputName();

When you publish your swf (I am just dropping that code on the timeline of an FLA) Ryan should appear in the output window.

The reason that we can access the myName variable directly is because we set the access modifier to public. In this case that is just fine but for the most part, you should never allow access directly to your instances variables. It can cause unexpected errors in your program and is bad Object Oriented Programming. So now that I told you to do something and then gave you shit for it lets change public to private to keep the riff raff out and see what happens

1178: Attempted access of inaccessible property myName through a reference with static type MyClass.

Just like Admiral Ackbar said – “It’s a trap“. When our code tries to access the now private variable the compiler fires the error above.

So how do we access that variable to change the name? Cue new title.

Getter and Setter Methods

There are two special types of methods we can use to return the value (get) and change the value (set) of the string variable. They are referred to as Getter and Setter methods.

In the example above we need a way to change the variable so we create a setter

public function set myNameSetter(newName:String):void{

myName  = newName;

}

If you notice, this method has the keyword set in the definition. This is a special keyword that tells Flash that this is a setter method and as such is called slightly different than regular methods. The method is also expecting an argument. In this case, a string.

Here is the code with the new myNameSetter method used to update the variable

import MyClass;

var newClass:MyClass = new MyClass();

newClass.myNameSetter = “Ryan”;

newClass.outputName();

I know, I know it looks almost identical. That is the confusing part, setters are called the same way that you would access the variable directly (which is why I changed the name to myNameSetter). The function is called and the argument is passed to it by using the equals sign.

The getter method is a little different

public function get myNameGetter():String{

return myName;

}

This method is similar in how it is called but there is an additional element. The return statement takes whatever the value of myName is and sends it back to the caller. (Notice that the function has String set as it’s return type). Here is the updated code

import MyClass;

var newClass:MyClass = new MyClass();

newClass.myNameSetter = “Ryan”;

var nameHolder:String = newClass.myNameGetter;

trace(nameHolder)

//will trace out Ryan

Again it looks very similar but in this case I have created another variable to hold the returned value of the myName string variable in order to store and then trace the value later.

So that is your intro to Access Modifiers and Getter and Setter methods. Hope it helped.

One Reply to “Getter ready, get Setter, Go!”

Leave a Reply

Your email address will not be published. Required fields are marked *