Kieran Quinn
Kieran Quinn

Reputation: 1115

How to return a variable from Public function

I'm trying to get away from using code on the main timeline but I'm struggling to understand how .as files and .fla files interact. For example, I'm trying to figure out how to pass a variable from the main timeline, to a public function, do some stuff to that variable and pass it back to the main timeline. I have an input text box on the frame and a simple button with a listener. I want to be able to input 00000 00 into the text box, and have 0.00 returned. Below is my code:

import flash.events.MouseEvent;
import convertToDecimal;
var inputText:String;
var outputText:String;

submit_btn.addEventListener(MouseEvent.CLICK, submit);

function submit(e:MouseEvent):void
{
    inputText = input_txt.text;
    new convertToDecimal(inputText);
    trace();
}

And here is the public function:

package
{
    import flash.sampler.StackFrame;
    import flash.events.MouseEvent;
    import fl.controls.Button;
    public class convertToDecimal
    {
        public function convertToDecimal(stringParmter:String)
        {
            var rex:RegExp = /[\s\r\n]+/gim;
            stringParmter = stringParmter.replace(/^\s+|\s+$/g, '');
            stringParmter = stringParmter.replace(rex,'.');
            stringParmter = stringParmter.replace(/^0+(?!\.|$)/, '');
            if ((stringParmter == "-----.--") || (stringParmter == "0"))
            {
                stringParmter = "      00";
            }
        }
    }
}

This is probably a really noob question but any help is appreciated.

Upvotes: 0

Views: 267

Answers (2)

ZuzEL
ZuzEL

Reputation: 13645

If you have class, in order to use it, you must construct its "copy" and assign it to the variable. Constructing your class is really easy:

new convertToDecimal(inputText); // does the constructing job

But what happen next? When your program goes to the next line, you constructed class will be loosed! You must assign it to variable, in order to keep it in memory:

var yourVariableName:convertToDecimal = new convertToDecimal(inputText);

Now you have your "copy" of class. OOP paradigm is good because you can create tons of "copies" really easily and then, each "copy" will live by its own live.


Now back to your question. It's not a secret that adding your code to the timeline is bad. Instead attach your class to your project and change it this way:

    package
{
    import flash.sampler.StackFrame;
    import flash.events.MouseEvent;
    import fl.controls.Button;

    public class Main
    {
        public function Main()
        {
            submit_btn.addEventListener(MouseEvent.CLICK, submit);

        }

        private function submit(e:MouseEvent):void
        {
            var inputText:String = input_txt.text;
            inputText = convertToDecimal(inputText);
            trace(inputText);
        }

        private function convertToDecimal(stringParmter:String):String
        {
            var rex:RegExp = /[\s\r\n]+/gim;
            stringParmter = stringParmter.replace(/^\s+|\s+$/g, '');
            stringParmter = stringParmter.replace(rex, '.');
            stringParmter = stringParmter.replace(/^0+(?!\.|$)/, '');
            if ((stringParmter == "-----.--") || (stringParmter == "0"))
            {
                stringParmter = "      00";
            }
            return stringParmter;
        }
    }
}

Upvotes: 2

Vesper
Vesper

Reputation: 18747

Parameters of simple types are passed by value, so in order to return a changed String, make your public function return String:

public function convertToDecimal(stringParmter:String):String {...}

Then, when you come up with a value that you want to be available outside, put a return <the value>; statement in your function. In order to capture the value returned from a function, assign it to a variable, the same one that's passed into it can be used.

stringParameter=convertToDecimal(stringParmter);

Also, if your AS3 file contains only the function, you can avoid wrapping it into a class, and declare directly "public function...".

package
{
    public function convertToDecimal(stringParmter:String):String
    {
            var rex:RegExp = /[\s\r\n]+/gim;
            var s:String;
            s = stringParmter.replace(/^\s+|\s+$/g, '');
            s = s.replace(rex,'.');
            s = s.replace(/^0+(?!\.|$)/, '');
            if ((s == "-----.--") || (s == "0"))
            {
                s = "      00";
            }
            return s;
    }
}

Upvotes: 0

Related Questions