AutoHotkey Community

It is currently May 27th, 2012, 1:01 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: November 14th, 2011, 5:18 pm 
Offline

Joined: June 17th, 2008, 7:51 am
Posts: 243
Code:
   _oArray:= object()
   String := "0001"
   _oXls := ComObjActive("Excel.Application")
   _oExcel := _oXls.ActiveWorkbook.ActiveSheet
   _oExcel.Range("A1:A2").NumberFormat := "@"
   _oExcel.Range("A1").Value := String ; --> 0001
   _oArray[String] := "ABC"
   _oExcel.Range("A2").Value := String  ; --> 1
   MsgBox,% String
I don't know if this is a bug, but after making "String" an index-key of an array, it delivers "1" instead of "0001" to Excel. And that's not what I expected. Also the index-key is "1" instead of "0001".

_________________
Greetings
Rog


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2011, 1:17 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
It is a design flaw which does not affect v2.

Since arrays (objects) treat integral numeric strings as integers when used as keys, indexing using a variable containing a numeric string causes a pure number to be cached in the variable. It has the same effect as the expression String + 0. When any variable containing a pure number is passed to a COM function, it is passed as a pure number instead of as a string (even if the variable also contains a string). You can force a particular type by using ComObjParameter.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2011, 9:12 am 
Thanks. So I can workaround this behavior with
Code:
   _oArray:= object()
   String := "0001"
   _oXls := ComObjActive("Excel.Application")
   _oExcel := _oXls.ActiveWorkbook.ActiveSheet
   _oExcel.Range("A1:A2").NumberFormat := "@"
   _oExcel.Range("A1").Value := String ; --> 0001
   _oArray[xx:=String] := "ABC"
   _oExcel.Range("A2").Value := String  ; --> 1
   MsgBox,% String


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2011, 7:44 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
I've just used an explicit concat with an empty string:

Code:
StrKey := "0123"
obj[StrKey ""] := "something"

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2011, 9:06 pm 
Offline

Joined: June 17th, 2008, 7:51 am
Posts: 243
That's better. Thanks. Now also the array-key stays 0001 instead of 1.

_________________
Greetings
Rog


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2011, 10:49 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Relying on obscure undocumented behaviour is not "better", especially when the behaviour exists only in v1, as in this case. If you want the leading zero to be part of the object key, you need to make the string non-numeric, e.g. by adding a prefix or postfix.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group