Argument checking

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.

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

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:


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);

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).

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

:battery:

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!!!


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);