> When creating an array or structure, CF allows things 
> like this:
> 
> <cfset myArray[1] = "foo" />
> <cfset myStruct.1 = "foo" />
> 
> But if the variable has been previously set to a different 
> data type (string in this case) like so:
> 
> <cfset myArray = "foo" />
> <cfset myStruct = "foo" />
> 
> <cfset myArray[1] = "foo" />
> <cfset myStruct.1 = "foo" />
> 
> the following error is received:
> 
> You have attempted to dereference a scalar variable of type 
> class java.lang.String as a structure with members.
> 
> You must specifically declare the variable as the new data 
> type to prevent errors:
> 
> <cfset myArray = "foo" />
> <cfset myStruct = "foo" />
> 
> <cfset myArray = arrayNew(1) />
> <cfset myStruct = structNew() />
> 
> <cfset myArray[1] = "foo" />
> <cfset myStruct.1 = "foo" />
> 
> Was this a problem in CF5? Is this an expected behavior or a 
> bug in CFMX? It should be mentioned that going the opposite 
> direction works just fine:
> 
> <cfset myArray[1] = "foo" />
> <cfset myStruct.1 = "foo" />
> 
> <cfset myArray = "foo" />
> <cfset myStruct = "foo" />

To the best of my knowledge (and I'm too lazy to actually test this right
now), in CF 5, you can't create a complex data object and create and
initialize a member within that object in one statement:

<!--- won't work in CF 5, will work in CFMX --->
<cfset myArray[1] = "foo">
<cfset myStruct.One = "foo">

So, in CF 5, this problem would never have arisen, because you'd have to use
ArrayNew or StructNew first anyway. I do agree with you that this is a bug
in CFMX, though - I would think the expected behavior would be for CF to
overwrite the scalar variable with the new complex variable, then initialize
the member. I don't think this is a serious bug, though, since it'll only
occur within arguably sloppy code - you shouldn't be overwriting variables
willy-nilly, and if you do overwrite a variable, it's probably a good idea
to explicitly overwrite the variable in a single step, then create and
initialize a member within it.

Also, I don't think you can do something like this, even in CFMX (although
again, I haven't tested it):

<cfset myStruct.1 = "foo">

With dot notation, I believe that the member name has to be a valid variable
name, which "1" isn't. So, you could do this:

<cfset myStruct["1"] = "foo">

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
voice: (202) 797-5496
fax: (202) 797-5444

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm

Reply via email to