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

发布于 2024-12-29 05:51:18 字数 3429 浏览 1 评论 0原文

我目前正在开发一个程序,可以将日语字符转换为英语字符,反之亦然。 但它并没有发挥多大作用,在过去的几天里,我一直在尽一切努力让它发挥作用,这可能是一些小愚蠢的问题,但我就是找不到它。我对这一切都很陌生,所以非常感谢您的帮助。

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

        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";
        }

我什至尝试过删除这个尝试自动查找角色类型并使用单选按钮执行此操作的功能,以便用户必须选择它,但不知怎的,它似乎做了几乎相同的事情......所以我现在完全困惑了,任何帮助都非常多 欢迎。

这是完整的代码:

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;
        }

I'm currently working on a program that converts japanese characters to english characters and the other way around.
It's not working much though, For the last few days I've been trying everything to try and get it to work, it's probably some small dumb problem but I just can't find it. I'm pretty new to all this so any help is appreciated.

Now the problem is that it only wants to convert Romaji characters, however if change some code, more specifically if I change the following from "if" to else if, then it converts hiragana and katakana but NOT 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";
        }

I've even tried removing this function that tries to automatically find what type the character is and do it with radiobuttons so that the user has to select it, but somehow it seems to do pretty much the same thing... So I'm totally confused at this point, any help is very much welcome.

Here is the full code:

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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2025-01-05 05:51:18

您的问题在于您对 Select 工作方式的误解。当没有匹配项时,Select 不会返回 null,因此您的第一个 if 始终为 true。相反,您需要检查是否有任何可以使用 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 == "" 位的全部内容肯定是不需要的。
  • 考虑为您的 char 类型创建一个 enum 类型。
  • 该方法可以静态化。
  • 考虑使用 switch 语句而不是 if (RequestedCharType == 1)
    &c.

Your problem is in your misunderstanding of how Select works. Select does not return null when there are no matches so your first if is always true. Instead you need to check whether there were any results which you can do with Enumerable.Any() (add using System.Linq):

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

Alternatively you can check the array length:

if (CharacterTable.Select("Romaji = '" + character + "'").Length > 0)
{
    fromtype = "Romaji";
}
else ...
  • I'm not sure what the fromType == "" bit is all about this is surely not needed.
  • Considering creating an enum type for your char types.
  • This method can be made static.
  • Consider using a switch statement instead of if (RequestedCharType == 1)
    &c.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文