回复:处理 JSL 类中的“On Change”实际上,我收到的错误消息是:
名称未解析:clb 正在访问或评估“clb”,clb/*###*/
对于那个很抱歉!
回复:处理 JSL 类中的“On Change”我认为班级成员有默认本地机制,不管你愿不愿意。 在
method1 = Method({},
this:window = New Window( "Test Window", clb = Col List Box( all, On Change( Show( clb << Get Items ) ) ) );
);
clb 在 method1 运行时分配,然后在 method1 返回时被遗忘。 很久以后,当 OnChange 方法运行时,找不到它。 如果你想要一个快速的、不完美的修复,让 clb 更全球化。
更好但更丑陋的修复方法需要嵌入 clb 的值来代替 clb。
Define Class( "exampleClass",
// create the instance variables
m_clb = .;
m_window = .;
m_BC = .;
_init_ = Method( {},
m_BC = Open( "$SAMPLE_DATA/Big Class.jmp" );
m_window = Empty();
);
method1 = Method( {},
m_clb = Col List Box( all );
// add the on change script after creating the box. The script needs
// to reference the box. Substitute the actual box that m_clb refers
// to for the xxx placeholder name, then evaluate the resulting script
Eval( Substitute(
Expr( m_clb << onChange( Show( xxx << getselected ) ) ),
Expr( xxx ),
m_clb
));
m_window = New Window( "Test Window", m_clb );
);
);
// Create an object of the class and try to use the function
testObject = New Object( exampleClass() );
testObject:method1();
这个可能不会做你期望的。 没有与 C++ this-> 等效的东西,你将很难在不造成内存泄漏的情况下发明它。 在上面的例子中,嵌入会更好这个而不是 clb,但你需要发明一种方法来获得这个.
我写了两个关于类的博客,如何使用定义类和使用 JSL 对象的函数式编程. 我曾多次尝试使用课程,但从未感到满意。 一个类做得很好的一件事是通过引用传递,这可能是使用它们的最佳理由。 在类实例中包装一个大数组可以将数组传递给用户编写的函数,而无需制作数组的副本。
如果只有 <这个参数,指向盒子,这会容易得多。
回复:处理 JSL 类中的“On Change”<< Set Function 可能是替代 << On Change 的好选择,因为它具有“this”作为函数参数。 根据您最终要做什么,我想说至少有三种不同的选择
1. 执行 Craige 的建议并评估对 Col List Box 的引用
2. 使用<< Set Function 然后你可以引用它(这将引用列列表框本身)。 这些确实有一些不同的行为,但它可能会影响您的应用程序
Define Class( "exampleClass",
m_clb = Empty();
m_window = .;
m_BC = .;
_init_ = Method( {},
m_BC = Open( "$SAMPLE_DATA/Big Class.jmp" );
m_window = Empty();
);
method1 = Method( {},
m_window = New Window("Test Window",
m_clb = Col List Box(
Datatable(m_BC),
all,
<< Set Function(function({this},
Show(this << get selected)
))
)
);
);
);
// Create an object of the class and try to use the function
testObject = New Object( exampleClass() );
testObject:method1();
3.使用窗口范围(脚本指南 > 编程方法 > 高级作用域和命名空间 > 作用域名称)
Define Class( "exampleClass",
m_window = .;
m_BC = .;
_init_ = Method( {},
m_BC = Open( "$SAMPLE_DATA/Big Class.jmp" );
m_window = Empty();
);
method1 = Method( {},
m_window = New Window("Test Window",
window:m_clb = Col List Box(
Datatable(m_BC),
all,
<< onChange(Show(window:m_clb << Get Selected))
)
);
);
);
// Create an object of the class and try to use the function
testObject = New Object( exampleClass() );
testObject:method1();在 JSL 中处理类中的“On Change”你好社区!
我在使用 Classes 时遇到了一些困难,所以我希望有人可以帮助我。
一些上下文:我正在编写一个脚本,该脚本分为两个:主脚本和函数脚本,然后后者包含在主脚本中。 我正在尝试使用类和命名空间,以确保仅在应该找到变量时才找到变量。 当尝试将“On Change”功能与 Col 列表框一起使用时,我的具体问题出现了。
我做了一个最小的工作示例来说明我的问题。 考虑主要代码:
Names Default To Here(1);
Clear log(); Clear Globals(); Clear Symbols();
// Include the class script
If( !Namespace Exists( "mainNamespace" ), New Namespace( "mainNamespace" ) );
mainNamespace:dir = Get Default Directory();
Include(mainNamespace:dir || "classScript.jsl");
// Create an object of the class and try to use the function
testObject = New Object( exampleClass());
testObject:method1();
然后是类脚本:
Names Default To Here(1);
If( !Namespace Exists( "classNamespace" ), New Namespace( "classNamespace" ) );
Define Class("exampleClass",
_init_ = Method( {},
this:BC = Open( "$SAMPLE_DATA/Big Class.jmp", Show(1) );
this:window = Empty();
);
method1 = Method({},
this:window = New Window( "Test Window", clb = Col List Box( all, On Change( Show( clb << Get Items ) ) ) );
);
);这个想法是打开一个窗口,您可以在其中从数据表中选择一些列,然后获取包含所选列的列表。 当我不使用类时,即如果我只是将脚本的类部分编写为“正常”代码,这效果很好。 上面的代码设法打开 Big Class 数据表以及带有 Col 列表框的窗口,但每当我单击某一列时,都会收到错误:
发送在访问或评估“发送”时需要可编写脚本的对象,clb << /*###*/Get Items /*###*/
是我做错了什么,还是不理解? 还是与“On Change”的编程方式有关? 任何帮助表示赞赏!
数据存取回复:在 JSL 类中处理“On Change”这是一个较旧的线程,但我想发布另一个解决方案,我认为在处理类内的窗口时非常方便。与 Jarmo 的解决方案类似,您可以使用 << Set Function 消息,但使用 Method 定义。它具有与 Function 大致相同的功能,并具有在类范围内进行计算的额外好处,这意味着您可以引用对象变量。
Define Class( "exampleClass",
m_clb = Empty();
m_window = .;
m_BC = .;
_init_ = Method( {},
m_BC = Open( "$SAMPLE_DATA/Big Class.jmp" );
m_window = Empty();
);
method1 = Method( {},
m_window = New Window("Test Window",
m_clb = Col List Box(
Datatable(m_BC),
all,
<< Set Function(Method({this},
Show(this << get selected);
Show(m_clb << get selected);
Show(m_window);
Show(m_BC);
))
)
);
);
);
// Create an object of the class and try to use the function
testObject = New Object( exampleClass() );
testObject:method1();“this”变量的工作方式与 Jarmo 解释的相同,但您也可以引用类中的任何其他变量。这里'this'相当于'm_clb'。
我发现以这种方式使用方法对于创建具有交互式组件的模块化窗口非常方便。