2

我在需要定义的类中有一组函数。每个都将不同的值传递给另一个函数:

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))

我需要以某种明智的方式定义所有功能。

4

1 回答 1

1

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()
}

在此处输入图像描述

于 2014-08-22T18:56:13.173 回答