我需要在一个类中定义一组函数。每个都将不同的值传递给另一个函数:
void function00(object self, taggroup tg) self.otherfunction(tg,0,0)
void function01(object self, taggroup tg) self.otherfunction(tg,0,1)
void function02(object self, taggroup tg) self.otherfunction(tg,0,2)
void function03(object self, taggroup tg) self.otherfunction(tg,0,3)
void function04(object self, taggroup tg) self.otherfunction(tg,0,4)
我有100个这样的函数,我不希望分别定义每个函数。考虑以上示例,我想做类似的事情:
for(number i=0; i<5; i++){
void function0+i(object self, taggroup tg) self.otherfunction(tg,0,i)
}
单靠它是行不通的。有什么建议么?
对于更多上下文,我在2的for循环内创建了一系列复选框,其中包含以下内容:
BOXinsides.DLGAddElement(DLGCreateCheckBox(label,0,"function"+i+j).DLGIdentifier("#function"+i+j))
而且我需要以某种明智的方式定义所有功能。
DigitalMicrograph脚本不允许这种类型的模板代码。但是,您可以通过将所有复选框项链接到相同的操作方法来解决问题。在TagGroup(它是复选框项目本身)中传递的操作方法的签名。您可以使用它来从中获取信息,例如,通过查看其标题之类的复选框属性:
class myUI : UIframe
{
void generalFunction( object self , tagGroup checkTg )
{
// checkTg is the taggroup of the checkbox which fired the method.
// Use its Title to get back the running value!
string label = checkTg.DLGGetTitle()
Result( "\n label of checkbox:" + label )
number i = val( right( label, len( label ) - 1 ) )
Result( "\n running index:" + i )
}
TagGroup CreateCheckboxes( object self )
{
TagGroup checkboxGroup = DLGCreateGroup()
for ( number i = 0 ; I < 5 ; i++ )
{
checkboxGroup.DLGAddElement( DLGCreateCheckBox( "C" + I , 0 , "generalFunction" ) )
}
return checkboxGroup
}
TagGroup CreateDLGTags( object self )
{
TagGroup dlg, dlgitems
dlg = DLGCreateDialog( "Test" , dlgitems )
dlgitems.DLGAddElement( self.CreateCheckboxes() )
return dlg
}
object Init( object self )
{
return self.super.init( self.CreateDLGTags() )
}
}
// MAIN SCRIPT calling the dialog
{
Object dlg = Alloc(myUI).Init()
dlg.pose()
}
您也可以直接将信息“附加”到复选框。与所有对话框项一样,复选框实际上只是特定的TagGroup对象,您可以在其中添加所需的任何对象。在下面的示例中,我添加了一个带有随机数的附加标签:
class myUI : UIframe
{
void generalFunction( object self , tagGroup checkTg )
{
// checkTg is the taggroup of the checkbox which fired the method.
// Use its Title to get back the running value!
string label = checkTg.DLGGetTitle()
Result( "\n label of checkbox:" + label )
number rnd
if ( checkTG.TagGroupGetTagAsNumber( "Random NR", rnd ) )
{
Result( "\n Random number:" + rnd )
}
}
TagGroup CreateCheckboxes( object self )
{
TagGroup checkboxGroup = DLGCreateGroup()
for ( number i = 0; I < 5 ; i++ )
{
TagGroup checkbox = DLGCreateCheckBox( "C" + I , 0 , "generalFunction" )
checkbox.TagGroupSetTagAsNumber( "Random NR", Random() )
checkboxGroup.DLGAddElement( checkbox )
}
return checkboxGroup
}
TagGroup CreateDLGTags( object self )
{
TagGroup dlg, dlgitems
dlg = DLGCreateDialog( "Test" , dlgitems )
dlgitems.DLGAddElement( self.CreateCheckboxes() )
return dlg
}
object Init( object self )
{
return self.super.init( self.CreateDLGTags() )
}
}
// MAIN SCRIPT calling the dialog
{
Object dlg=Alloc(myUI).Init()
dlg.pose()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句