【已解决】如何在命令的 OutputVar 参数中直接引用 Object 的属性?

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

wz520
Posts: 29
Joined: 01 Jan 2014, 21:32
Location: China

【已解决】如何在命令的 OutputVar 参数中直接引用 Object 的属性?

16 Jan 2015, 06:46

大家好。 :D

AutoHotkey 用了好几年了,但基本都在用 Basic 兼容语法, 最近为了写程序更方便想学学 L 里的面向对象编程的语法,于是发现某些AHK命令的 OutputVar 参数无法直接引用 Object 的属性。

比如 INIRead:
IniRead, OutputVar, Filename [, Section, Key, Default]
第一个参数是 OutputVar,当 OutputVar 是某个 Object 的属性时,就会出现错误:

Code: Select all

config := {}
IniRead, config.color, test.ini, settings, color
会报错。
Error at line 2.

The following variable name contains an illegal character:
"config.color"

The program will exit.

尝试过在 config.color 前加 % 以表示这是个表达式(INIWrite 用这个办法即可将 Object 的属性写到 INI 文件中),但还是报错:
Error at line 2.

Line Text: % config.color
Error: This parameter contains a variable name missing its ending percent sign.

The program will exit.

本以为只有 INIRead 是这样,后来发现好像所有帮助文件里名为 OutputVar 的参数都是如此。比如 RegRead, Random 等等

当然用个中间变量即可解决这种问题。
但是很麻烦不是嘛? :wtf:

求问有无解决办法? :?:
Last edited by wz520 on 20 Jan 2015, 19:44, edited 1 time in total.
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: 如何在命令的 OutputVar 参数中直接引用 Object 的属性?

16 Jan 2015, 07:45

I do not understand anything :D
but:

Code: Select all

config := {}

IniRead, config_color, test.ini, settings, color

config.color := config_color
Variable names: Variable names are not case sensitive (for example, CurrentDate is the same as currentdate). Variable names may be up to 253 characters long and may consist of letters, numbers and the following punctuation: # _ @ $
so you cant name your variable config.color
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
wz520
Posts: 29
Joined: 01 Jan 2014, 21:32
Location: China

Re: 如何在命令的 OutputVar 参数中直接引用 Object 的属性?

16 Jan 2015, 19:39

jNizM wrote:I do not understand anything :D
but:

Code: Select all

config := {}

IniRead, config_color, test.ini, settings, color

config.color := config_color
Variable names: Variable names are not case sensitive (for example, CurrentDate is the same as currentdate). Variable names may be up to 253 characters long and may consist of letters, numbers and the following punctuation: # _ @ $
so you cant name your variable config.color

Thanks for your reply~~ :D

I want to use a property of an object on an "OutputVar" parameter of a command.
But AHK reports an error.
Your solution is just what I thought, and it works well, but it uses a temporary var.
I wonder if there is anyway to do this without using a temporary var. :?:
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: 如何在命令的 OutputVar 参数中直接引用 Object 的属性?

19 Jan 2015, 01:36

yes.. if you create a function for IniRead

eg (not tested)

Code: Select all

config := {}
config.color := IniRead("C:\Temp\testfile.ini", "settings", "color")


IniRead(Filename := "", Section := "", Key := "")
{
    IniRead, OutputVar, % Filename, % Section, % Key
    return OutputVar
}
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
wz520
Posts: 29
Joined: 01 Jan 2014, 21:32
Location: China

Re: 如何在命令的 OutputVar 参数中直接引用 Object 的属性?

19 Jan 2015, 21:18

This is a better solution, but...
I have to write new functions if I want to do such usage on other commands, such as RegRead, Random, etc...

It would be better if there is a general-purpose function...e.g.

Code: Select all

GetOutputVar(cmdname, params*)
{
}
But...It seems this is impossible since AHK has no "eval" function that evaluates AHK commands from a string at runtime.

Maybe my demand is too high...? :eh:

Anyway, thanks again, jNizM~~ :clap:
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: 如何在命令的 OutputVar 参数中直接引用 Object 的属性?

20 Jan 2015, 01:55

貌似不行..
因为传递的是求值后的变量名(variable name),而非引用,由命令内部再解析..
而且ByRef也不支持传入对象属性的..
这个即便在函数式语言中也无法支持..

感兴趣的话,可以试试AHK V2,面向对象的语法更易用,有翻译的文档。
GetOutputVar(cmdname, params*)在V2应该可以实现..
我为人人,人人为己?
wz520
Posts: 29
Joined: 01 Jan 2014, 21:32
Location: China

Re: 如何在命令的 OutputVar 参数中直接引用 Object 的属性?

20 Jan 2015, 19:44

AHK V2 的语法好像有很多地方和 1 不兼容,现在还是算了吧,啥时候有时间再慢慢啃。

4楼的方案够用了,这帖子还是结了吧...

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 14 guests