C#用||切换字符串

时间:2018-01-19 12:21:29

标签: c#

我试图对2个功能进行仔细检查,但我怎么能通过开关来实现:

        pwdStrength.SetPassword(text_passmysql.Text);
        pwdStrength1.SetPassword(text_adminpass.Text);

        PassStrength strSql = (PassStrength)Enum.Parse(typeof(PassStrength), pwdStrength.GetPasswordStrenght1());
        PassStrength strAdm = (PassStrength)Enum.Parse(typeof(PassStrength), pwdStrength1.GetPasswordStrenght1());

        PassStrength lowest = (PassStrength)Math.Min((Int32)strSql, (Int32)strAdm);

        if ((Int32)lowest < (Int32)PassStrength.Good)
        {
            // Show an error message to the user
            MessageBox.Show("A password do MySQL é muito fraca, escolha um mais forte.\nApenas pode alterar a password MySQL para uma mais forte, caso contrário não será guardada.", "Configurações do Programa", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            // Password deemed strong enough, allow user to be added to database etc
        }

如何处理上述示例?

由于

编辑:搞定了!谢谢@Nyerguds。

编辑完代码后,我设法使用@Nyerguds发布的解决方案。

谢谢大家

解决方案:

    enum PassStrength
    {
        Unchecked = 0, // Enums should always have an empty value for initialisation.
        VeryWeak = 1,
        Weak = 2,
        Good = 3,
        Strong = 4,
        VeryStrong = 5
    }

    public string GetPasswordStrenght1()
    {
        string valor = GetPasswordStrength();

        switch(valor)
        {
            case "Very Weak":
                return Convert.ToString(PassStrength.VeryWeak);
            case "Weak":
                return Convert.ToString(PassStrength.Weak);
            case "Good":
                return Convert.ToString(PassStrength.Good);
            case "Strong":
                return Convert.ToString(PassStrength.Strong);
            case "Very Strong":
                return Convert.ToString(PassStrength.VeryStrong);
            default:
                return Convert.ToString(PassStrength.Unchecked);
        }
    }

新功能:

def _validate_custom_validation(self, custom_validation, field, value):
  if field == "field1":
    f1 = value
    f2 = self.document.get('field2')

  if field == "field2":
    f1 = self.document.get('field1')
    f2 = value

  if custom_validation and not is_validate(f1, f2):
    self._error(field, "validation failed...")

5 个答案:

答案 0 :(得分:0)

pwdStrength.SetPassword(text_passmysql.Text); pwdStrength1.SetPassword(text_adminpass.Text); checkPassword(pwdStrength.GetPasswordStrength()); checkPassword(pwdStrength1.GetPasswordStrength()); public checkPassword(var pwdStrength){ switch (pwdStrength) { case "Very Weak": case "Weak": // Show an error message to the user MessageBox.Show(...); break; case "Good": case "Strong": case "Very Strong": // Password deemed strong enough, allow user to be added to database etc //do something break; } } 返回布尔值,所以基本上你的情况只有真/假。您可以将Content-Disposition置于循环中,遍历密码,一次只传递1个值或将其作为方法提取。

export class TableColumn {
  columnName: string;
  isToFilter: boolean;
  isToSort: boolean;
  isLink: boolean;
  linkUrl: string;
  columnData: any[];

constructor(
    columnName: string,
    isToFilter: boolean,
    isToSort: boolean,
    isLink: boolean,
    linkUrl: string,
    columnData: any[]) {
    this.columnName = columnName;
    this.isToFilter = isToFilter;
    this.isToSort = isToSort;
    this.isLink = isLink;
    this.linkUrl = linkUrl;
    this.columnData = columnData;
  }
}

答案 1 :(得分:0)

你不能用开关做到这一点,你需要if

var strength1 = pwdStrength1.GetPasswordStrength();
var strength2 = pwdStrength2.GetPasswordStrength();

if ("Weak" == strength1 || "Weak" == strength2) {
  // ... do something
}
else if ("Strong" == strength1 || "Strong" == strength2) {
  // ... do something
}
// ...

答案 2 :(得分:0)

“||” operator是条件 OR运算符。它用于true或false条件(布尔值)。

一个例子:

if(x == y || x == z)

读作:“如果(这是真的,或者这是真的)”

因此,您使用if语句会混淆交换机的使用。

The conditional OR operator

The Switch statement

答案 3 :(得分:0)

我的建议是,让GetPasswordStrength()返回一个Enum而不是String。无论如何,String上的Switch-Case结构完全是假的;他们只是在内部进行一堆字符串比较,而实际 Switch-Case实际上是一个非常优雅和超高效的操作,在CPU级别。

所以,首先,制作你的枚举,并指定一个“质量”。每个项目的价值:

public Enum PassStrength
{
    Unchecked = 0, // Enums should always have an empty value for initialisation.
    VeryWeak = 1,
    Weak = 2,
    Good = 3,
    Strong = 4,
    Very Strong = 5
}

返回而不是你的字符串。

然后,由于枚举是内部整数,您只需检查两个密码之间的最低值:

pwdStrength.SetPassword(text_passmysql.Text);
pwdStrength1.SetPassword(text_adminpass.Text);

PassStrength strSql = pwdStrength.GetPasswordStrength()
PassStrength strAdm = pwdStrength1.GetPasswordStrength()

PassStrength lowest = (PassStrength)Int32.MinValue((Int32)strSql, (Int32)strAdm);

if (lowest < (Int32)PassStrength.Good)
    // Give message that at least one of the passwords is too weak
Else
    // passwords are both at least "Good".

如果你想在上提供特定的消息(老实说,这似乎比说&#34更加用户友好;你的一个密码太弱了#34;没有说明哪一个),那么显然没有其他方法可以做到这一点,而不是单独检查两个。

在相关的说明中,您真的需要pwdStrength对象吗?如果只检查强度,这似乎是静态函数可以做的事情。

答案 4 :(得分:0)

搞定了!谢谢@Nyerguds。

编辑完代码后,我设法使用@Nyerguds发布的解决方案。

谢谢大家

解决方案:

    pwdStrength.SetPassword(text_passmysql.Text);
    pwdStrength1.SetPassword(text_adminpass.Text);

    PassStrength strSql = (PassStrength)Enum.Parse(typeof(PassStrength), pwdStrength.GetPasswordStrenght1());
    PassStrength strAdm = (PassStrength)Enum.Parse(typeof(PassStrength), pwdStrength1.GetPasswordStrenght1());

    PassStrength lowest = (PassStrength)Math.Min((Int32)strSql, (Int32)strAdm);

    if ((Int32)lowest < (Int32)PassStrength.Good)
    {
        // Show an error message to the user
        MessageBox.Show("A password do MySQL é muito fraca, escolha um mais forte.\nApenas pode alterar a password MySQL para uma mais forte, caso contrário não será guardada.", "Configurações do Programa", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        // Password deemed strong enough, allow user to be added to database etc
    }

新功能:

enum PassStrength
{
    Unchecked = 0, // Enums should always have an empty value for initialisation.
    VeryWeak = 1,
    Weak = 2,
    Good = 3,
    Strong = 4,
    VeryStrong = 5
}

public string GetPasswordStrenght1()
{
    string valor = GetPasswordStrength();

    switch(valor)
    {
        case "Very Weak":
            return Convert.ToString(PassStrength.VeryWeak);
        case "Weak":
            return Convert.ToString(PassStrength.Weak);
        case "Good":
            return Convert.ToString(PassStrength.Good);
        case "Strong":
            return Convert.ToString(PassStrength.Strong);
        case "Very Strong":
            return Convert.ToString(PassStrength.VeryStrong);
        default:
            return Convert.ToString(PassStrength.Unchecked);
    }
}