字符串值为空,则不要在条件

时间:2018-03-13 11:56:16

标签: c# .net microsoft-ui-automation

private string FindValue(AutomationElement f_Element)
{
    object l_PriorityFirstValue = GetValue(0);
    object l_PrioritySecondValue= GetValue(1);
    object l_PriorityThirdValue GetValue(2);       

    AutomationElementCollection elementCollection =
        AutomationElement.RootElement.FindAll(TreeScope.Subtree, new PropertyCondition("1", l_PriorityFirstValue));

    if(elementCollection.count==1)
    {
       return "True";
    }
    else
    {
       ////Now try to find with additional call
        elementCollection = AutomationElement.RootElement.FindAll(TreeScope.Subtree,
            new AndCondition(
                new PropertyCondition(1, l_PriorityFirstValue),
                new PropertyCondition(2, l_PrioritySecondValue)));

        if (elementCollection.Count == 1)
        {
            return "True";
        }
        else
        {
            ////Now try to find with additional call 
            elementCollection = AutomationElement.RootElement.FindAll(TreeScope.Subtree,
                new AndCondition(
                    new PropertyCondition(1, l_PriorityFirstValue),
                    new PropertyCondition(2, l_PrioritySecondValue),
                    new PropertyCondition(3, l_PriorityThirdValue)));

            if (elementCollection.Count == 1)
            {
                return "True";
            }
            else
            {
                 return "false";
            }
        }
    }        
}

private string GetValue(int i)
{
    //get value using parameter i from db.
}

我有上面的代码,如果条件为空,我不想在条件中使用Priority值。

例如:假设l_PrioritySecondValue变空,那么我不想在查询中使用new PropertyCondition(2, l_PrioritySecondValue)

在这种情况下应该是什么逻辑? Priority值是动态的。任何值都将为空,第一,第二或第三。

1 个答案:

答案 0 :(得分:-1)

//第一段代码是你想做的或不想做的但是更容易让列表传递第二个函数

 List<Object> PriorityConditions=new List<Object>();    
 for(int i=0;i<LengthOfPriorities(3 in your case);i++)
  {
    PriorityConditions.Add(GetValue(i));
  }

编写一个函数来获取条件(ReturnType:AndCondition接受相同的项目列表)

     List<ReturnType> GetConditions(PriorityConditions)
       {
        List<ReturnType> conditions=new List<ReturnType>();
        for(int i=0;i<PriorityConditions.count;i++)
        {
            if(!PriorityConditions[i])   // Check conditions valid for your object (null or empty)
                conditions.Add(PriorityConditions[i]);          
        }

        return conditions;
  }

这段代码将变为此

 elementCollection = AutomationElement.RootElement.FindAll(TreeScope.Subtree,
            new AndCondition(GetConditions(PriorityConditions)));