Hi,
the differences in type checking between "i am a string" and new String ("i
am a string") and xml and other objects is fairly annoying.
ie:
trace (typeof("string")); //traces string
trace (typeof(new String("string"))); //traces object
Same goes for other objects such as xml etc its all fairly inconsistent.
Im using this wrapper class, makes my life hell of a lot easier.
trace (TypeChecker.isType ("foo", String)); //true
trace (TypeChecker.isType (new String("foo"), String)); //true
trace (TypeChecker.isType (myXML, XML)); //true
Give it a spin.
In addition, I see you are mixing languages in your code, this happens to us
sometimes but only when the clients business model contains dutch terms that
are hard to translate or communicate after translation. In all other cases,
mixing languages is very confusing and errorprone in my experience and as
such bad practice. Feel free to ignore this statement:)
greetz
JC
/**
* Checks types in a consistent way no matter whether the passed object is a
wrapper object or a primitive object.
*
* TypeChecker prevents having to know about flash weirdness:
*
* 'test' instanceof String ==> false
* typeof('test') == 'string' ==> true
* TypeChecker.checkType ('test', String) ==> true
*
* new String() instanceof String ==> true
* typeof(new String()) == 'string' ==> false
* TypeChecker.checkType (new String(), String) ==> true
*
* Works for instances and instances of subclasses.
*
* The cast type check ie 'if (String(pParam) == null)' is not reliable
since it works differently under MTASC and
* Flash. Array() is a cast in MTASC but a array creation in Flash.
*
* $Id: TypeChecker.as 148 2007-08-10 09:47:11Z hansw $
*
* @author J.C. Wichman, TriMM Interactive Media (www.trimm.nl /
[EMAIL PROTECTED]),
* ObjectPainters.com (blog.objectpainters.com /
[EMAIL PROTECTED])
*/
class nl.trimm.util.TypeChecker {
/**
* Checks the given object for its type.
*
* @param pObject the object to type check
* @param pType the type to validate given as a class ie String or MovieClip
* @return true if pObject is of type pType
*/
public static function isType (pObject:Object, pType:Object):Boolean {
var lResult:Boolean = pObject instanceof pType;
if (lResult == true) return true;
switch (pType) {
case Boolean:
return typeof(pObject)=="boolean";
case String:
return typeof(pObject)=="string";
case Number:
return typeof(pObject)=="number";
}
return false;
}
/**
* Of type number and not NaN
*/
public static function isNaturalNumber (pObject:Object):Boolean {
return isType (pObject, Number) &&
(
Number(pObject) != Number.NaN &&
Number(pObject) != Number.NEGATIVE_INFINITY &&
Number(pObject) != Number.POSITIVE_INFINITY
);
}
/**
* Checks if all elements in the given array match the given type.
*
* @param pArray the array to check
* @param pType the allowed type
* @return true if all elements match the given type
*/
public static function isArrayOfType (pArray:Array, pType:Object):Boolean {
var lResult:Boolean = true;
//walk through array, as soon as the result doesnt match the given type
break
for (var i:Number = 0; i < pArray.length; i++) {
lResult = isType (pArray[i], pType);
if (!lResult) break;
}
return lResult;
}
/**
* Checks if all elements in the array match at least one of the given types
*
* @param pArray array of elements to check
* @param pTypes each element has to match with at least one of the types in
this array
* @return true if each element matches at least one of the types in the
type array
*/
public static function isArrayOfTypes (pArray:Array, pTypes:Array):Boolean
{
var lResult:Boolean = null;
//walk through array, as soon as the result doesnt match the given type
break
for (var i:Number = 0; i < pArray.length; i++) {
lResult = false;
//walk through type array as soon as one matches break
for (var lTypeIndex:Number = 0; lTypeIndex < pTypes.length; lTypeIndex++)
{
lResult = lResult || isType (pArray[i], pTypes[lTypeIndex]);
if (lResult) break;
}
if (!lResult) break;
}
return lResult;
}
public static function arrayContainsType (pArray:Array,
pType:Object):Boolean {
var lResult:Boolean = false;
//walk through array, as soon as the result doesnt match the given type
break
for (var i:Number = 0; i < pArray.length; i++) {
lResult = isType (pArray[i], pType);
if (lResult) break;
}
return lResult;
}
/**
* Returns value if it is of the given type, null otherwise.
*
* @param pObject the object to type check
* @param pType the type to validate given as a class ie String or MovieClip
* @return pObject is of type pType, null otherwise
*/
public static function ensureType (pObject:Object, pType:Object):Object {
return isType (pObject, pType)?pObject:null;
}
}
On 8/23/07, Cedric Muller <[EMAIL PROTECTED]> wrote:
>
> look, maybe, here ...
>
> // just declaring the dataType won't be enough
> var xmlData:XML;
> trace(xmlData instanceof XML); // <- outputs: false
>
> // declaring the dataType and assigning an instance of XML
> var xmlData:XML = new XML();
> trace(xmlData instanceof XML); // <- outputs: true
>
>
> typeof doesn't and will never return XML as a result. Look up the
> table in the help panel for 'typeof':
>
> String:string
> Movie clip:movieclip
> Button:object
> Text field:object
> Number:number
> Boolean:boolean
> Object:object
> Function:function
>
>
> hth,
> Cedric
>
> > Sorry, typo
> > The fla says 'var xmlData:XML'.
> > Any other suggestions?
> > Thanks,
> > Willem
> >
> > Op 23-aug-2007, om 10:16 heeft Cedric Muller het volgende geschreven:
> >
> >> and if you do:
> >>
> >> var xmlData:XML;
> >>
> >> ??
> >> Cedric
> >>
> >>> var xmlData:xml;
> >>
> >
> > =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
> > Geografiek is a Dutch, Utrecht-based map and chart design company.
> > Willem van den Goorbergh can be contacted by telephone: (+31)
> > 30-2719512 or cell phone: (+31)6-26372378
> > or by fax: (+31)302719687
> > snail mail: Hooghiemstraplein 89 3514 AX UTRECHT
> > Visit our website at: http://www.geografiek.nl
> > =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
> >
> >
> > _______________________________________________
> > [email protected]
> > To change your subscription options or search the archive:
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > Brought to you by Fig Leaf Software
> > Premier Authorized Adobe Consulting and Training
> > http://www.figleaf.com
> > http://training.figleaf.com
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com