C# c语言中与DataSet相关的奇怪问题
我目前正在做一个程序,将日语字符转换成英语字符,反之亦然。 虽然效果不太好,但在过去的几天里,我一直在想方设法让它发挥作用,这可能是个小问题,但我就是找不到。我对这一切都很陌生,所以非常感谢您的帮助 现在的问题是,它只想转换罗马字符,但是如果更改一些代码,更具体地说,如果我将下面的if更改为else if,那么它将转换平假名和片假名,而不是罗马字符C# c语言中与DataSet相关的奇怪问题,c#,localization,if-statement,internationalization,C#,Localization,If Statement,Internationalization,我目前正在做一个程序,将日语字符转换成英语字符,反之亦然。 虽然效果不太好,但在过去的几天里,我一直在想方设法让它发挥作用,这可能是个小问题,但我就是找不到。我对这一切都很陌生,所以非常感谢您的帮助 现在的问题是,它只想转换罗马字符,但是如果更改一些代码,更具体地说,如果我将下面的if更改为else if,那么它将转换平假名和片假名,而不是罗马字符 string fromtype = ""; // Determines what type the charact
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执行的任何结果。使用System.Linq进行任何添加: 或者,您可以检查数组长度:
if (CharacterTable.Select("Romaji = '" + character + "'").Length > 0)
{
fromtype = "Romaji";
}
else ...
我不确定fromType==位是什么意思,这肯定是不需要的。
正在考虑为您的字符类型创建枚举类型。
这种方法可以是静态的。
考虑使用Switter语句,而不是使用RealDestCabyType=1
&c。
FWIW他们被称为罗马字符,因此“罗马字”的数字是阿拉伯的然而-谢天谢地,我们不必使用罗马数字。好的,谢谢!我会去试试的,很抱歉,我对这一切还是很陌生,所以只要程序正常运行,我会很高兴的,呵呵,没必要道歉。我们曾经都在那里,现在离那里不远了。是的,这很有效^^非常感谢,我几天来一直在努力解决这个问题^不用担心。祝第2版好运:
if (CharacterTable.Select("Romaji = '" + character + "'").Length > 0)
{
fromtype = "Romaji";
}
else ...