c#中与数据集有关的奇怪问题
本文关键字:问题 数据集 | 更新日期: 2023-09-27 18:21:40
我目前正在开发一个程序,该程序可以将日语字符转换为英语字符,反之亦然。不过,它并没有起到多大作用。在过去的几天里,我一直在尽一切努力让它发挥作用,这可能是一些愚蠢的小问题,但我就是找不到它。我对这一切都很陌生,所以任何帮助都很感激。
现在的问题是,它只想转换罗马字,然而,如果更改一些代码,更具体地说,如果我将以下内容从"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;
}
您的问题在于对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
类型 - 此方法可以设置为静态
- 考虑使用CCD_ 9语句而不是CCD_;c