2014-06-23 15 views
0

我有三个标志,可能是True或False。我需要为每个可能的标志组合显示一个图标。由于有三个标志,所以组合起来就有八个可能的状态。 (如下所示,其中粗体表示真。)检查三个标志的所有可能组合的最有效逻辑是什么?

ABC

甲BC

Ç

AB Ç

ABÇ

Ç

B C

A B C

是否有检查标志,以减少不必要的检查,以使用一种有利的控制流? (这会改变由该标志是可能来打开或关闭?)


编辑:

例如,当我看着简单标志A和B,我的控制流是 -

if(A & B) 
{ 
    // Display icon for A+B 
} 
else if (A) 
{ 
    // Display icon for A 
} 
else if (B) 
{ 
    // Display icon for B 
} 
+0

(如果有这个问题,请让我知道,我会努力提高它的一个问题。) – Eilidh

回答

2

我将设置一个8位变量,允许位2,1,0存储您的标志状态。

然后

switch(variablename) 
{ 
    case 0: 

    break; 

    .. 
    .. 

    case 7: 

    break; 
}  
0
protected void grdDemo_ItemDataBound(object sender, GridItemEventArgs e) 
{ 
    if (!(e.Item is GridDataItem) || (e.Item.DataItem == null)) 
    { 
     return; 
    } 

    /////////////////// 
    //// Solution 1 /// 
    /////////////////// 

    // If it is possible to manipulate image name 
    MyClass M1 = e.Item.DataItem as MyClass; 
    ImageButton imgStatus = (ImageButton)e.Item.FindControl("imgStatus"); 

    StringBuilder sb = new StringBuilder(); 

    sb.Append(M1.A ? "1" : "0"); 
    sb.Append(M1.B ? "1" : "0"); 
    sb.Append(M1.C ? "1" : "0"); 

    string ImageName = "imgStaus" + sb.ToString() + ".jpg"; 
    imgStatus.ImageUrl = "~/path/" + ImageName; 


    /////////////////// 
    //// Solution 2 /// 
    /////////////////// 

    ImageName = string.Empty; 

    double FlagCount = 0; 
    FlagCount += Math.Pow((M1.A ? 0 : 1) * 2, 3); 
    FlagCount += Math.Pow((M1.B ? 0 : 1) * 2, 2); 
    FlagCount += Math.Pow((M1.B ? 0 : 1) * 2, 1); 
    var intFlagCount = (int)FlagCount; 

    switch (intFlagCount) 
    { 
     case 0: 
      ImageName = "imgStausFFF.jpg"; 
      break; 
     case 1: 
      ImageName = "imgStausFFT.jpg"; 
      break; 
     case 2: 
      ImageName = "imgStausFTF.jpg"; 
      break; 
     case 3: 
      ImageName = "imgStausFTT.jpg"; 
      break; 
     case 4: 
      ImageName = "imgStausTFF.jpg"; 
      break; 
     case 5: 
      ImageName = "imgStausTFT.jpg"; 
      break; 
     case 6: 
      ImageName = "imgStausTTF.jpg"; 
      break; 
     case 7: 
      ImageName = "imgStausTTT.jpg"; 
      break; 
    } 

    imgStatus.ImageUrl = "~/path/" + ImageName; 

    //////DONE!!!!!!!!!! 

} 

class MyClass 
{ 
    public bool A { get; set; } 
    public bool B { get; set; } 
    public bool C { get; set; } 
} 
相关问题