Oops, I deleted part of a sentence, it should have been:

"or you're trying to do more and, say, cater for a case where you want
the empty string and null to be equivalent as uninitialized states in
your program"?


________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Peter Farland
Sent: Thursday, April 05, 2007 11:23 AM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders] Re: question about string equality



For AS3, I think it is fine to use the new operator in general - I was
clutching-at-straws as to why something might be different in your
scenario (largely because I know in AS2 there was a difference between
"" and new String() as there was the concept of primitives and object
forms of the string type and the object form wouldn't serialize
correctly in AMF 0, for instance).
 
To be honest, I've lost track of the original question in this thread
and am not sure whether you're just looking for the best way to compare
strings in AS3 or you're trying to do more and, say, cater for a case
where you want the empty string being to be equivalent to an
uninitialized state in your program?
 
I know one scenario that can be confusing is this:
 
var s1:String = "";
var s2:String = null;
 
if (s1)
{
    trace("s1 was true");    
}
 
if (s2)
{
    trace("s2 was true");
}
 
Neither of these trace statements will execute. I always avoid this
short-cut syntax because it leads to further confusion that all
non-zero-length strings evaluate to true, and a string like "false" will
just be seen as a String of length > 0 and hence true. To be safe, I
always write the condition I'm trying to test explicitly...
 
if (s1 != null && s1 == "true")
{
    //...
}
 
Pete
 
 
 

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of simonjpalmer
Sent: Thursday, April 05, 2007 11:02 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: question about string equality



huh, thanks again Pete, I am obviously have my Java roots showing. 
This is the sort of thing that it is hard to get to know about AS3
without coding for some time, and I am relatively new to it.

How do I distinguish between types that need initialisation through
new and those that don't? For instance an ArrayCollection needs a new
whereas a Number (and apparently a String) does not. I naively
considered them all to be objects and as such need initialising,
although I am clearly not religious about it in my code.

I have also long wondered about primitives in AS3. What is an int? 
What primitives are there? What is a Boolean?

Is there a good reference source for this sort of AS3 information? I
think I have travelled far enough that it is about time I went back to
the beginning.

--- In flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
, "Peter Farland" <[EMAIL PROTECTED]> wrote:
>
> I would guess that === is actually faster than == as the latter has to
> check whether it needs to perform any casting before checking
equality. 
> 
> Out of curiousity, have you tried to use "" instead of new String() to
> initialize category (it's unconventional to use new String() in AS3)?
> 
> Otherwise, you could send the complete source in a bug for the team to
> take a look.
> 
> Pete
> 
> ________________________________
> 
> From: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
[mailto:flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
] On
> Behalf Of simonjpalmer
> Sent: Wednesday, April 04, 2007 12:42 PM
> To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> 
> Subject: [flexcoders] Re: question about string equality
> 
> 
> 
> Good point, I didn't include the declarations. 
> 
> They are both strongly typed Strings, I don't use objects anywhere in
> my code.
> 
> co is a custom AS object of type "Competitor", here's the declaration
> of the name member:
> 
> public var name:String;
> 
> oc is a custom AS object of type "ObjectCategory" and here is the
> declaration of the category member:
> 
> public var category:String = new String();
> 
> The strong typing answers the question about whether they just happen
> to contain strings.
> 
> Other than the fact that they belong to custom objects I have written,
> there is nothing peculiar about either the string variables or their
> contents.
> 
> I don't think they are in a custom namespace, but to be honest I'm not
> exactly sure what that means, so I can't say with certainty that they
> aren't. I think the answer is no.
> 
> co.name gets populated by various means, either though a user gesture
> in a custom page or by retrieval from a java data adaptor to my
> server. oc.category is populated programmatically during execution of
> the code in question.
> 
> I appreciate you guys looking at this. Right now I have it working as
> I expect but it is a bit worrying that I need to do the comparison in
> this way only in this instance. That says to me that I don't properly
> understand something.
> 
> If I want to check equality of the content of two strings should I
> always be testing valueOf()? 
> 
> What is the overhead of using ===?
> 
> --- In flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com>
> , "Peter Farland" <pfarland@> wrote:
> >
> > What are the type declarations of the properties sc.name and
> > sc.category? Do they just happen to hold String values or are they
> typed
> > to enforce that they hold String values? Is there anything else
unique
> > about these properties? Are they in a custom namespace? Are they
> > read-only? How were they populated in the first place?
> > 
> > ________________________________
> > 
> > From: flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com>
> [mailto:flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com>
> ] On
> > Behalf Of simonjpalmer
> > Sent: Monday, April 02, 2007 6:14 PM
> > To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com> 
> > Subject: [flexcoders] question about string equality
> > 
> > 
> > 
> > take a look at this code snippet...
> > 
> > 01 // check one doesn't already exist with this name
> > 02 bFound = false;
> > 03 for (isc = 0; isc < ss.scenarios.length && !bFound; isc++)
> > 04 {
> > 05 sc = Scenario(ss.scenarios.getItemAt(isc));
> > 06 if (sc.name.valueOf() == oc.category.valueOf()) bFound = true;
> > 07 }
> > 08 if (!bFound)
> > 09 {
> > 10 // Make a new scenario
> > 11 sc = PlanPointFactory.makeScenario(uli, null, true, false);
> > 12
> > 13 // add it to the snapshot
> > 14 ss.addScenario(sc);
> > 15
> > 16 // add it to the local array of categories
> > 17 oc.objects.push(sc);
> > 18 }
> > 
> > line 06 is the offending line.
> > 
> > if I have:
> > 
> > 06 if (sc.name == oc.category) bFound = true;
> > 
> > the bFound flag never gets set true. I have to have the valueOf()
> > function in order for the equality to fire correctly.
> > 
> > This is not what I expected. I thought that regular equality would
> > have sufficed here since sc.name and oc.category are both Strings.
> > 
> > Why am I wrong and why do I need valueOf()?
> >
>



 

Reply via email to