c#中与DataSet相关的奇怪问题

时间:2012-01-26 12:00:38

标签: c# localization if-statement internationalization

我目前正致力于将日语字符转换为英文字符的程序,反之亦然。 虽然它不起作用,但是最近几天我一直在努力尝试让它发挥作用,这可能是一些小问题,但我找不到它。我对这一切都很陌生,所以对任何帮助表示赞赏。

现在的问题是它只想转换罗马字符,但是如果我改变了一些代码,更具体地说,如果我将以下内容从“if”更改为else if,那么它会转换平假名和片假名而不是romaji ..

        string fromtype = "";

        // Determines what type the character is currently
        // && fromtype == "" added to avoid weird unexplainable errors...
        if (CharacterTable.Select("Romaji = '" + character + "'") != null && fromtype == "")
        {
            fromtype = "Romaji";
        }
        else if (CharacterTable.Select("Hiragana = '" + character + "'") != null && fromtype == "")
        {
            fromtype = "Hiragana";
        }
        else if (CharacterTable.Select("Katakana = '" + character + "'") != null && fromtype == "")
        {
            fromtype = "Katakana";
        }

我甚至尝试删除此功能,试图自动找到该字符的类型,并使用radiobuttons进行操作,以便用户必须选择它,但不知何故它似乎做了几乎相同的事情......所以我在这一点上完全感到困惑,非常欢迎任何帮助。

以下是完整代码:

public string CheckCharacter(string character, int RequestedCharType)
        {
            // RequestedCharType
            // 1 = Romaji
            // 2 = Hiragana
            // 3 = Katakana

            //-- Instantiate the data set and table
            DataSet CharacterDatabase = new DataSet();
            DataTable CharacterTable = CharacterDatabase.Tables.Add();

            //-- Add columns to the data table
            CharacterTable.Columns.Add("Romaji", typeof(string));
            CharacterTable.Columns.Add("Hiragana", typeof(string));
            CharacterTable.Columns.Add("Katakana", typeof(string));


            //-- Add rows to the data table
            CharacterTable.Rows.Add("a", "あ", "ア");
            CharacterTable.Rows.Add("i", "い", "イ");


            // Sets fromtype to the type the character(s) currently is/are
            string fromtype = "";

            // Determines what type the character is currently
            // && fromtype == "" added to avoid weird unexplainable errors...
            if (CharacterTable.Select("Romaji = '" + character + "'") != null && fromtype == "")
            {
                fromtype = "Romaji";
            }
            else if (CharacterTable.Select("Hiragana = '" + character + "'") != null && fromtype == "")
            {
                fromtype = "Hiragana";
            }
            else if (CharacterTable.Select("Katakana = '" + character + "'") != null && fromtype == "")
            {
                fromtype = "Katakana";
            }



           // generates a new variable to store the return in
           DataRow[] filteredRows = CharacterTable.Select(fromtype + " = '" + character + "'");

            // Return the converted character in the requested type
            foreach (DataRow row in filteredRows)
            {
                if (RequestedCharType == 1)
                {
                    return row["Romaji"].ToString();
                }
                if (RequestedCharType == 2)
                {
                    return row["Hiragana"].ToString();
                }
                if (RequestedCharType == 3)
                {
                    return row["Katakana"].ToString();
                }
            }


            // if it couldn't find the character, return the original character
            return character;
        }

1 个答案:

答案 0 :(得分:0)

您的问题在于您对Select如何运作的误解。如果没有匹配项,则Select不会返回null,因此您的第一个if始终为真。相反,您需要检查是否有任何可以使用Enumerable.Any()(添加using System.Linq)的结果:

if (CharacterTable.Select("Romaji = '" + character + "'").Any())
{
    fromtype = "Romaji";
}
else ...

或者你可以检查数组长度:

if (CharacterTable.Select("Romaji = '" + character + "'").Length > 0)
{
    fromtype = "Romaji";
}
else ...
  • 我不确定fromType == ""位是什么,肯定不需要。
  • 考虑为您的字符类型创建enum类型。
  • 此方法可以是静态的。
  • 请考虑使用switch语句代替if (RequestedCharType == 1) & c。