VBScript:如何遍历多个对象属性并将对象属性作为函数参数传递?

时间:2016-11-08 22:28:45

标签: function loops vbscript properties

我有三个关于VBScript的问题:

  1. 如何遍历多个对象属性:.Lang,.Time,.Cal等?
  2. 如何使用属性或属性作为函数参数传递对象(请参阅函数LoopThruAssocArrays(p_objArray1,p_arrArray1,p_objArray2,p_arrArray2))?
  3. 如何将strObj字符串转换为对象?
  4. 目标是设置和读取(记录)以下对象迭代属性,值和 每个对象变量的Program.Settings.Numbers.Prec。任何想法如何以最有效的方式释放它?关于Q1我使用Eval / Execute听说过反思和课堂概念,但老实说我不知道​​如何采用它(我不是程序员)。

    'Set g_objProgram.Settings.Regional.Lang = U_Lang_en
    'Set g_objProgram.Settings.Regional.Lang = U_Lang_de
    'Set g_objProgram.Settings.Regional.Lang = U_Lang_es
    'Set g_objProgram.Settings.Regional.Lang = U_Lang_it
    '...
    'Set g_objProgram.Settings.Regional.Time = U_Time_loc
    'Set g_objProgram.Settings.Regional.Time = U_Time_zon
    'Set g_objProgram.Settings.Regional.Time = U_Time_cet
    'Set g_objProgram.Settings.Regional.Time = U_Time_gmt
    '...
    'Set g_objProgram.Settings.Regional.Cal = U_Cal_gre
    'Set g_objProgram.Settings.Regional.Cal = U_Cal_heb
    'Set g_objProgram.Settings.Regional.Cal = U_Cal_mus
    'Set g_objProgram.Settings.Regional.Cal = U_Cal_bud
    '...
    'Set g_objProgram.Settings.Numbers.Prec = 0...3
    'Set g_objProgram.Settings.Numbers.Delim = True/False ' not used yet
    

    存储值使用带数组的字典:

    Function Code1 ()
    Dim objSettingsRegional : objSettingsRegional ("Scripting.Dictionary")
    objSettingsRegional ("Lang") = Array (U_Lang_en, U_Lang_de, U_Lang_es, U_Lang_it, ...)
    objSettingsRegional ("Time") = Array (U_Time_loc, U_Time_zon, U_Time_cet, U_Time_gmt, ...)
    objSettingsRegional ("Cal") = Array (U_Cal_gre, U_Cal_heb, U_Cal_mus, U_Cal_bud, ...)
    
    arrSettingsNumbersPrec = Array (0,1,2,3)
    arrSettingsNumbersDelim = Array (True, False)
    
    For Each SettingsRegionalKey In objSettingsRegional.Keys
      For Each SettingsRegionalItem In objSettingsRegional.Items
         For Each SettingsNumbersPrecItem In arrSettingsNumbersPrecItem
    
           Dim strObj : strObj = "g_objProgram.Settings.Regional." & SettingsRegionalKey
    
           'converting string to obj???
           Dim objDict : objDict("Scripting.Dictionary")
           objDict. Add "StrToObj", strObj
           Dim item : item = objDictItem("StrToObj")
    
           Set item = SettingsRegionalItem
           Dim result1 : result1 = item
    
           Set g_objSettings.Numbers.Prec = SettingsNumbersPrecItem
           Dim result2 : result2 = g_objSettings.Numbers.Prec
    
         Next
      Next
    Next
    End Function
    

    使用具有多个If ... ElseIf语句的数组的其他伪代码示例:

    Function Code2 ()
    arrSettingsRegional = Array ("Lang", "Time", "Cal", ...)
    arrSettingsRegionalLang = Array (U_Lang_en, U_Lang_de, U_Lang_es, U_Lang_it, ...)
    arrSettingsRegionalTime = Array (U_Time_loc, U_Time_zon, U_Time_cet, U_Time_gmt, ...)
    arrSettingsRegionalCal = Array (U_Cal_gre, U_Cal_heb, U_Cal_mus, U_Cal_bud, ...)
    
    arrSettingsNumbersPrec = Array (0,1,2,3)
    arrSettingsNumbersDelim = Array (True, False)
    
    For Each SettingsRegionalKey In objSettingsRegionalKey
      If SettingsRegionalKey = "Lang" Then
        Dim strObj: strObj = "g_objProgram.Settings.Regional." & SettingsRegionalKey
        Call LoopThruAssocArrays (strObj, arrSettingsRegionalLang, objProgram.Settings.Numbers.Prec, arrSettingsNumbersPrec)
      ElseIf SettingsRegionalKey = "Time" Then
        strObj = "g_objProgram.Settings.Regional." & SettingsRegionalKey
        Call LoopThruAssocArrays (strObj, arrSettingsRegionalTime, g_objProgram.Settings.Numbers.Prec, arrSettingsNumbersPrec)
      ElseIf SettingsRegionalKey = "Cal" Then
        strObj = "g_objProgram.Settings.Regional." & SettingsRegionalKey
        Call LoopThruAssocArrays (strObj, arrSettingsRegionalCal, g_objProgram.Settings.Numbers.Prec, arrSettingsNumbersPrec)
      End If
    Next
    
    End Function
    
    Function LoopThruAssocArrays(p_objArray1, p_arrArray1, p_objArray2, p_arrArray2)
      For Each p_arrArray1Item In p_arrArray1
        Set p_objArray1 = p_arrArray1Item
        Dim result1 : result1 = p_objArray1
          For Each p_arrArray2Item In p_arrArray2
            Set p_objArray2 = p_arrArray2Item
            Dim result2 : result2 = p_objArray2
          Next
      Next
    End Function
    

0 个答案:

没有答案