MyVar := A_Index. Is MyVar integer? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 04:12

There is the code filling Excel cells:

Code: Select all

if !Excel := Excel_Get()
   Return

XLSht := Excel.ActiveSheet

loop 2 {
   row := A_Index
   loop 2 {
      col := A_Index
      XLSht.Cells(row, col).Value := "test"
   }
}

Excel_Get(WinTitle := "ahk_class XLMAIN")  {
   ControlGet, hwnd, hwnd,, Excel71, %WinTitle%
   if !hwnd
      return
   Window := AccObjectFromWindow(hwnd, -16)
   Loop  {
      try Application := Window.Application
      catch
         ControlSend, Excel71, {esc}, %WinTitle%
   } Until !!Application
   return Application
}

AccObjectFromWindow(hWnd, idObject = 0)
{
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", Str, "oleacc", Ptr)
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", Str, idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, Ptr, &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", Ptr, hWnd, UInt, idObject, Ptr, &IID, PtrP, pAcc) = 0
      Return ComObjEnwrap(VT_DISPATCH, pAcc, F_OWNVALUE)
}
It doesn't work, produces an error 0x800a03ec — NAME_NOT_FOUND. But it starts to work by replacing col := A_Index with col := A_Index + 0.
What's wrong with col := A_Index?
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 04:56

This will fix it

Code: Select all

loop 2 {
	row := (A_Index)
	loop 2 {
		col := (A_Index)
		XLSht.Cells(row, col).Value := "test"
	}
}

ref:
https://www.autohotkey.com/boards/viewtopic.php?p=3187#p3187
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 05:58

lexikos wrote: v := A_Index isn't evaluated as an expression.
I don't quite understand what this means.
jNizM wrote: This will fix it

Code: Select all

loop 2 {
	row := (A_Index)
	loop 2 {
		col := (A_Index)
		XLSht.Cells(row, col).Value := "test"
	}
}
But this works with row := A_Index too. Can you clarify?
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 06:16

Maybe @lexikos can assist here.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
mikeyww
Posts: 27046
Joined: 09 Sep 2014, 18:38

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 07:13

I was puzzled as well. I also found that Round(A_Index) worked, presumably for the same reason-- but unsure what the reason is! Seems like this A_Index is a float, but not the other one? Hard to figure out.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 07:18

A_Index - is string.
But it seems like a bug.

Code: Select all

XLSht.Cells("1", 1).Value := "test"   ; ok
XLSht.Cells(1, "1").Value := "test"   ; not ok
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 07:50

malcev wrote: A_Index - is string.
Perhaps it's true. But this works:

Code: Select all

loop 2 {
   row := A_Index
   loop 2 {
      XLSht.Cells(row, A_Index).Value := "test"
   }
}
lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: MyVar := A_Index. Is MyVar integer?  Topic is solved

22 Apr 2021, 07:54

AutoHotkey v1 does not fully support the concept of types. For another example, see Caching. Improving this area while not breaking older scripts in some way is unlikely, and not worth the trouble. If you want clarity, use v2.

v := A_Index is evaluated as a pure variable assignment, as is v = %A_Index%. These are effectively resolved to an assignment command (like SetEnv), with v as an output variable and A_Index as an input variable. All built-in variables return strings in AutoHotkey v1, because that is how they were designed. Prior to v1.0.48, even numbers assigned to variables were only stored as strings.

When A_Index or A_EventInfo is used in an expression, the variable itself is actually not queried. Instead, the expression evaluator directly uses the integer value of g->mLoopIteration or g->EventInfo. The comments in the source code (from v1.0.48.01 and v1.0.48.02) indicate this was done purely for performance. No other variables have this optimization, therefore all other built-in variables produce strings even when used in an expression.
The ColumnIndex can be provided either as a numeric index or as a column address string as in A1-notation, i.e. "A" refers to the numeric index 1 and "AA" to 27.
Source: Range.Item property (Excel) | Microsoft Docs
It appears that RowIndex must always be a number, so the standard conversion of Variant to number is applied and the method doesn't care that you passed a string. By contrast, a string ColumnIndex has different meaning to a number.
teadrinker wrote:But this works:
Because A_Index evaluates to a pure integer when evaluated within an expression.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 07:56

Thanks for the explanation
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
mikeyww
Posts: 27046
Joined: 09 Sep 2014, 18:38

Re: MyVar := A_Index. Is MyVar integer?

22 Apr 2021, 08:17

Thanks. I knew the problem was Microsoft all along! :lol:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 462 guests