FDT Get/Set Template

January 19th, 2008

I am working on a new class and was getting tired of typing out getters and setters in FDT 3 so I created 2 new templates to help me out.

If you go to preferences->FDT->Templates you can add, edit, and remove the built in code templates. These are a great time saver and if you have never worked with them take a look at the ones that are included. If you would like to have a simple Getter and Setter template use the follow code:

public function get ${name} (): ${type} {
return _${name};
}

This is for the getter. You fill in “name” as the value of the variable you want to use as a getter. Since I use a naming convention where all my private vars have “_” infront of them you will see in the return it adds one in automatically. $() is Eclipses version of a variable so as you type in the name for the getter it will automatically show up in both places. The last value is the “type”. When you call a template in the FDT editor it highlights the values and you can use the tab key to move from one template variable to the next.

public function set ${name} (value: ${type}):void {
_${name} = value;
}

This is for the setter and it follows the same convention as above. Hope this shaves of valuable seconds from your coding!

I just thought of one thing after I made this post. If you combine these two templates into one you can create a single get/set template. Now it will fill in everything automatically for you after the first few variables get values. Save even more time!

/**
* Get function for _${name}.
* @return _${name} : ${type}
*/
public function get ${name} () : ${type} {
return _${name};
}

/**
* Set function for _${name}.
* @param value : ${type}
*/
public function set ${name} (value : ${type}):void {
_${name} = value;
}

This example will set up both the getter and setter along with some nice commenting.

3 Responses to “FDT Get/Set Template”

  1. enzuguri Says:

    Hi Jesse, recently found your blog and as a young graduate finding it really useful (especially your mvc stuff). Anyways I was going around creating similar templates to these in FDT, until I looked in the tips and tricks section of the documentation. If you declare the variable at the top of your code and then invoke quick assist (so command-1 or crtl-1) with the name selected you will get a drop-down that allows you to create getters and setters. It will even work out underscores as well.

  2. FlashBum Says:

    Thats a great tip, I will update the post. Its amazing what you learn when you RTFM.

  3. Al Says:

    @enzuguri : the getter/setter’s aren’t *proper* get set pairs - they will take a variable someVar and create two function getSomeVar and setSomeVar. Unless I’ve missed something??

    I’ve adapted the above template to also create the private var:

    private var _${name} : ${type};
    public function get ${name}() : ${type} {
    return _${name};
    }
    public function set ${name}(value : ${type}) : void {
    _${name} = value;
    }

    I’ve set it’s shortcut to GS, so pressing ctrl/cmd - space then typing “GS” will insert the template - enter variable name and type and your done.

    Just need to figure out how to create the proper get/set pairs from an existing variable….

Leave a Reply