# Argument checking

**URL:** <https://forum.kirupa.com/t/argument-checking/184644>\
**Category:** Uncategorized\
**Created:** [April 6, 2006, 2:56pm UTC](https://forum.kirupa.com/t/argument-checking/184644 "2006-04-06T14:56:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwopitz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jwopitz/32/1556_2.png) [@jwopitz](https://forum.kirupa.com/u/jwopitz)\
**Post date:** [April 6, 2006, 2:56pm UTC](https://forum.kirupa.com/t/argument-checking/184644/1 "2006-04-06T14:56:39Z")

</div>

Hi,  
I have what I think is both a simple problem and a simple solution, but I just can’t seem to get it to work.

I have a class that has multiple remoting call type methods (ie. it talks to the server and fires other methods on a response or fault). One thing I a need to do is to make sure that all parameters passed to these methods are \*\*not undefined \*\*or \*\*null.

\*\*So here is the proposed solution:  
ActionScript Code:  
[FONT=Courier New][LEFT][COLOR=#000000] **var** [/COLOR] a:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]“ah”[/COLOR];  
[COLOR=#000000] **var** [/COLOR] b:[COLOR=#0000FF]String[/COLOR];  
[COLOR=#000000] **var** [/COLOR] c:[COLOR=#0000FF]String[/COLOR];  
[COLOR=#000000] **function** [/COLOR] mainFunc [COLOR=#000000]([/COLOR]x, y, z[COLOR=#000000])[/COLOR]:[COLOR=#0000FF]Void[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]arguments[/COLOR].[COLOR=#0000FF]caller[/COLOR][COLOR=#000000][[/COLOR][COLOR=#0000FF]arguments[/COLOR][COLOR=#000000]][/COLOR] = argChk COLOR=#000000[/COLOR];  
[COLOR=#0000FF]trace[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#FF0000]“function arguments: “[/COLOR] + [COLOR=#0000FF]arguments[/COLOR][COLOR=#000000])[/COLOR];  
testParam COLOR=#000000[/COLOR] [COLOR=#808080]_//traces “ah”_[/COLOR]  
testParam COLOR=#000000[/COLOR] [COLOR=#808080]_//traces undefined_[/COLOR]  
testParam COLOR=#000000[/COLOR];[COLOR=#808080]_//traces undefined_[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000] **function** [/COLOR] argChk COLOR=#000000[/COLOR]:[COLOR=#0000FF]Array[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] newParam:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Array[/COLOR] COLOR=#000000[/COLOR];  
[COLOR=#808080]_//_[/COLOR]  
[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i [COLOR=#0000FF]in[/COLOR] param[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#808080]_//trace (param_);\*[/COLOR]  
[COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR]param[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] == [COLOR=#0000FF]undefined[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]trace[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#FF0000]“parameter “[/COLOR] + i + [COLOR=#FF0000]” is undefined”[/COLOR][COLOR=#000000])[/COLOR];  
param[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] = [COLOR=#FF0000]””[/COLOR];  
[COLOR=#000000]}[/COLOR]  
newParam.[COLOR=#0000FF]unshift[/COLOR] COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]return[/COLOR] newParam;  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000] **function** [/COLOR] testParam COLOR=#000000[/COLOR]:[COLOR=#0000FF]Void[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]trace[/COLOR] COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[/LEFT]  
[/FONT]

And this is what I want instead:  
ActionScript Code:  
[FONT=Courier New][LEFT][COLOR=#000000] **var** [/COLOR] a:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]“ah”[/COLOR];  
[COLOR=#000000] **var** [/COLOR] b:[COLOR=#0000FF]String[/COLOR];  
[COLOR=#000000] **var** [/COLOR] c:[COLOR=#0000FF]String[/COLOR];  
[COLOR=#000000] **function** [/COLOR] mainFunc [COLOR=#000000]([/COLOR]x, y, z[COLOR=#000000])[/COLOR]:[COLOR=#0000FF]Void[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]arguments[/COLOR].[COLOR=#0000FF]caller[/COLOR][COLOR=#000000][[/COLOR][COLOR=#0000FF]arguments[/COLOR][COLOR=#000000]][/COLOR] = argChk COLOR=#000000[/COLOR];  
[COLOR=#0000FF]trace[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#FF0000]“function arguments: “[/COLOR] + [COLOR=#0000FF]arguments[/COLOR][COLOR=#000000])[/COLOR];  
testParam COLOR=#000000[/COLOR] [COLOR=#808080]_//traces “ah”_[/COLOR]  
testParam COLOR=#000000[/COLOR] [COLOR=#808080]_//traces “”_[/COLOR]  
testParam COLOR=#000000[/COLOR];[COLOR=#808080]_//traces “”_[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000] **function** [/COLOR] argChk COLOR=#000000[/COLOR]:[COLOR=#0000FF]Array[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] newParam:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Array[/COLOR] COLOR=#000000[/COLOR];  
[COLOR=#808080]_//_[/COLOR]  
[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i [COLOR=#0000FF]in[/COLOR] param[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#808080]_//trace (param_);\*[/COLOR]  
[COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR]param[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] == [COLOR=#0000FF]undefined[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]trace[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#FF0000]“parameter “[/COLOR] + i + [COLOR=#FF0000]” is undefined”[/COLOR][COLOR=#000000])[/COLOR];  
param[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] = [COLOR=#FF0000]””[/COLOR];  
[COLOR=#000000]}[/COLOR]  
newParam.[COLOR=#0000FF]unshift[/COLOR] COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]return[/COLOR] newParam;  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000] **function** [/COLOR] testParam COLOR=#000000[/COLOR]:[COLOR=#0000FF]Void[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]trace[/COLOR] COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[/LEFT]  
[/FONT]

As you can see, I can change the parameters once inside the function to be checked, however those are simply references now and not the same parameters being passed onto other nested functions.

If I can get this to work, it will save hours on parameter checking that would otherwise require that I copy and paste a checker in about a jillion places on this project. Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![jwopitz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jwopitz/32/1556_2.png) [@jwopitz](https://forum.kirupa.com/u/jwopitz)\
**Post date:** [April 6, 2006, 2:59pm UTC](https://forum.kirupa.com/t/argument-checking/184644/2 "2006-04-06T14:59:44Z")

</div>

I thought I would go ahead and post the .fla too so you guys don’t have to copy & paste this stuff.

---

<div class="post-metadata">

**Author:** ![jwopitz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jwopitz/32/1556_2.png) [@jwopitz](https://forum.kirupa.com/u/jwopitz)\
**Post date:** [April 6, 2006, 9:36pm UTC](https://forum.kirupa.com/t/argument-checking/184644/3 "2006-04-06T21:36:35Z")

</div>

Well I know I am just talking to myself here but I figure I will post my efforts and hopefully my solution so that others may benefit.

Ok so I first I need to post some updated code:

```auto

var a:String = "ah";
var b:String;
var c:String;
function mainFunc (x, y, z):Void
{
	trace ("function arguments: " + arguments);
	argChk (arguments)
	nestedFunction (arguments);
	//
	for (var i in arguments) {
		trace (arguments*);
	}
}
function argChk (param:Object)
{
	var newParam:Array = new Array ();
	//
	for (var i = 0; i < param.length; i++) {
		if (param* == undefined) {
			trace ("parameter " + i + " is undefined");
			param* = "";
		}
		//newParam.unshift (param*); 
	}
	//return param;
}
function nestedFunction (a:Object):Void
{
	trace ("testParam");
	trace (arguments);
	for (var i in a) {
		trace (a*);
	}
	trace("====================");
	for (var i in arguments[0]) {
		trace (arguments[0]*);
	}
	trace ("end testParam");
}
mainFunc (a, b, c, 5);

```

---

<div class="post-metadata">

**Author:** ![jwopitz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jwopitz/32/1556_2.png) [@jwopitz](https://forum.kirupa.com/u/jwopitz)\
**Post date:** [April 6, 2006, 9:40pm UTC](https://forum.kirupa.com/t/argument-checking/184644/4 "2006-04-06T21:40:31Z")

</div>

From the above posting I found a few things out:

- when passing the arguments from the mainFunc into the nestedFunction, I am only passing one parameter which is the arguments object.
- I need to be able to pass the arguments obj as seperate items into the nestedFunction as seperate parameters.
- I wonder if there is something out there that can intercept the object and break it apart so that an objects items can then be passed to a targeted function, and done so in such a way that it could repeated dynamically for any number of parameters.

ok so I will keep posting (to myself).

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [April 6, 2006, 10:00pm UTC](https://forum.kirupa.com/t/argument-checking/184644/5 "2006-04-06T22:00:21Z")

</div>

```auto
function mainFunc():Void {
 nestedFunction.apply(null, arguments);
}
function nestedFunction():Void {
 trace(arguments);
 trace(arguments.length);
}
mainFunc(1, 2, 3, "a", "b", "c");

```

:battery:

---

<div class="post-metadata">

**Author:** ![jwopitz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jwopitz/32/1556_2.png) [@jwopitz](https://forum.kirupa.com/u/jwopitz)\
**Post date:** [April 6, 2006, 10:29pm UTC](https://forum.kirupa.com/t/argument-checking/184644/6 "2006-04-06T22:29:49Z")

</div>

COOL!!! wow, I knew there had to be a simple means of doing this. Ok so here is my code and a big thanks to Canadian. Thanks Canadian!!!

```auto

var a:String = "ah";
var b:String;
var c:String;
//
function mainFunc (x, y, z):Void
{
	//showing that the vars above aren't all defined
	trace(arguments)
	//storing the parameters after they have been checked as an obj
	argObj = argChk(arguments);
	//the magic happens here
	nestedFunction.apply (null, argObj);

}
function argChk (param:Array):Array
{
	var newParam:Array = new Array ();
	//
	for (var i = 0; i < param.length; i++) {
		if (param* == undefined) {
			param* = "parameter " + i + " wasn't defined and now it is";
		}
		newParam* = param*;
	}
	return newParam;
}

function nestedFunction (a, b):Void
{	
	trace("nestedFunc");
	//this if statement is here to test to make sure that
	//there were indeed multiple parameters and not one
	//obj being passed into this function
	if (b == undefined) {
		trace ("must define parameter");
	}
	for (i in arguments) {
		trace (arguments*);
	}
}
mainFunc (a, b, c, 5);

```
