Assign function parameter to a var....?

I may really be showing my ignorance here but I’m stuck on what seems should be a simple solution…

I need to take a parameter that is being passed to a function and assign it to a variable for use in other functions in the script.

Any help would be greatly appreciated.

var myVar:String

function sso(url)
{
    //some code here to assign parameter url to myVar....????
   trace (url);
   
}


sso(someStringHere);

private var myVar:String

function sso(url)
{
myVar = url;
trace (url);
}

sso(someStringHere);

I still can’t seem to access the var from other functions…

var myVar:String

function sso(url):void
{
    //some code here to assign parameter url to myVar....????
   trace (url);
   myVar = url;
   
}

function hello(e:Event):void
{
   trace (myVar);
   //Traces myVar as null.....?
}


sso(someStringHere);

myComboBox.addEventListener(Event.CHANGE, hello);


It’s almost as if it’s assigning the var inside the function sso and keeping it there… ie, not a global var though the document…? Am I missing something.?

Thanks for the response!

I am not that good at that yet either but have you tried using an Array because if Im not mistaken by you saying var myVar:String can only be used once. Im not %100 sure but maybe look into Arrays.

So once I assign a variable a value… I cannot change that value?? Surely this cannot be true… kinda goes against the word variable…

Anyways, I ‘worked around’ this by creating a dynamic text field and assigning the .text of that field with the parameter. Once the text field is populated it’s accessible from anywhere within the project.

This doesn’t seem like the ‘correct’ way to do things but it works.

Note: I set the text field to myTextField.visible = false; so it would not be visible on the stage…

you tried to trace the var before you assigned the new value, so it would trace null! =D

EDIT:: I made this example for you !!


var test:String = "http://www.google.co.uk";
var myVar:String;

function sso(url):void {
    //some code here to assign parameter url to myVar....????
    myVar = url; //Make you you assign the value BEFORE you try and trace it
    trace(url);//This will be the first google in the output panel
}

sso(test);

function mouseClicked(evt:Event) {
    trace(myVar);//The rest of the google's will come from this

}

box1_mc.addEventListener(MouseEvent.CLICK, mouseClicked); // box1_mc is a simple movieclip I made

[QUOTE=craig.clayton;2339403]I am not that good at that yet either but have you tried using an Array because if Im not mistaken by you saying var myVar:String can only be used once. Im not %100 sure but maybe look into Arrays.[/QUOTE]

This is not correct. var simply defines a new variable accessible at the current scope level. The scope of the variable depends on where it is defined, and whether it is defined as public/private/protected (i believe if you don’t state this it defaults to protected). For example if you define a var within a for loop it is accessible only in that iteration of the for loop. If you define a var in the root of a class it is accessible for anything within that class. If you define it as public it is accessible to anything that can reference your class. And unless you define it as a static variable if it can be accessed it can be changed. If you want to control the way it is changed you need to make it a private variable and use getters and setters.