运算符'=='不能应用于类型为“字符”和“串”的操作数 [英] Operator '==' cannot be applied to operands of type 'char' and 'string'
本文介绍了运算符'=='不能应用于类型为“字符”和“串”的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我也有它里面的KeyChar活动的注册应用程序,它的伟大工程!但是,当我给相同的代码行本应用它给了我操作员'=='/'!='不能应用于类型为字符和串的操作数
似乎无法弄清楚,为什么它工作在其他应用程序,但不是在这里!
任何帮助深表感谢!
私人无效textBox1_KeyPress(对象发件人,KeyPressEventArgs E)
{
的SqlConnection DBConnection的=新的SqlConnection(数据源=数据库;初始目录=综合管理信息系统;集成安全性=真);
的SqlCommand CMD =新的SqlCommand();
对象的returnValue;
串txtend = textBox1.Text;
//字符串lastChar = txtend.Substring(txtend.Length - 1);
如果(e.KeyChar ==L)
{
DBConnection.Open();
}
如果(DBConnection.State == ConnectionState.Open)
{
如果(textBox1.Text.Length = 7!)回报;
{
//cmd.CommandText =(SELECT姓氏+,+ FIRST_NAME +'\t('+ major_key +从名称')\t其中id = @名称);
cmd.CommandText =(SELECT姓氏+,+ FIRST_NAME从名字其中id = @名称);
cmd.Parameters.Add(新的SqlParameter(姓名,textBox1.Text.Replace(@L,)));
cmd.CommandType = CommandType.Text;
cmd.Connection = DBConnection的;
// sqlConnection1.Open();
的returnValue = cmd.ExecuteScalar()+\t(+ textBox1.Text.Replace(@L,)+);
DBConnection.Close();
如果(listBox1.Items.Contains(的returnValue))
{
为(INT N = listBox1.Items.Count - 1; N> = 0; --N )
{
串removelistitem = returnValue.ToString();
如果(listBox1.Items [N]的ToString()包含(removelistitem)。)
{
listBox1.Items.RemoveAt(N);
//listBox1.Items.Add(removelistitem++超时+时间);
}
}
}
,否则
listBox1.Items.Add(的returnValue);
System.IO.StreamWriter SAVEFILE =新System.IO.StreamWriter(fullFileName);
的foreach(在listBox1.Items对象的项目)
SaveFile.WriteLine(item.ToString());
SaveFile.Flush();
SaveFile.Close();
textBox1.Clear();
如果(listBox1.Items.Count!= 0){DisableCloseButton(); }
,否则
{
EnableCloseButton();
}
Current_Attendance_Label.Text =目前+ listBox1.Items.Count.ToString()+到齐了。
e.Handled = TRUE;
}
}
//////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////
,否则
//////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////
{
的returnValue = textBox1.Text.Replace(@*,);
如果(e.KeyChar ==*)回报;
{
如果(listBox1.Items.Contains(的returnValue))
{
的for(int N = listBox1.Items.Count - 1; N> = 0; - N)
{
串removelistitem = returnValue.ToString();
如果(listBox1.Items [N]的ToString()包含(removelistitem)。)
{
//listBox1.Items.RemoveAt(n);
}
}
}
,否则
listBox1.Items.Add(的returnValue);
textBox1.Clear();
System.IO.StreamWriter SAVEFILE =新System.IO.StreamWriter(fullFileName);
的foreach(在listBox1.Items对象的项目)
SaveFile.WriteLine(item.ToString());
SaveFile.Flush();
SaveFile.Close();
如果(listBox1.Items.Count!= 0){DisableCloseButton(); }
,否则
{
EnableCloseButton();
}
Current_Attendance_Label.Text =目前+ listBox1.Items.Count.ToString()+到齐了。
e.Handled = TRUE;
}
}
}
解决方案
使用的字符。单引号
定义字符
,而双引号定义一个
字符串
:
如果(e.KeyChar =='L ')
I have a registration application which has the "KeyChar" event inside it, and it works great ! but when i give the same lines of code in this application it gives me Operator '=='/'!=' cannot be applied to operands of type 'char' and 'string'
Can't seem to figure out why it works in the other application but not here! Any help is much appreciated !
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
Object returnValue;
string txtend = textBox1.Text;
//string lastChar = txtend.Substring(txtend.Length - 1);
if (e.KeyChar == "L")
{
DBConnection.Open();
}
if (DBConnection.State == ConnectionState.Open)
{
if (textBox1.Text.Length != 7) return;
{
//cmd.CommandText = ("SELECT last_name +', '+ first_name +'\t ('+major_key+')\t' from name where id =@Name");
cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
cmd.CommandType = CommandType.Text;
cmd.Connection = DBConnection;
// sqlConnection1.Open();
returnValue = cmd.ExecuteScalar() + "\t (" + textBox1.Text.Replace(@"L", "") + ")";
DBConnection.Close();
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
listBox1.Items.RemoveAt(n);
//listBox1.Items.Add(removelistitem+" " +TimeOut+ Time);
}
}
}
else
listBox1.Items.Add(returnValue);
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName);
foreach (object item in listBox1.Items)
SaveFile.WriteLine(item.ToString());
SaveFile.Flush();
SaveFile.Close();
textBox1.Clear();
if (listBox1.Items.Count != 0) { DisableCloseButton(); }
else
{
EnableCloseButton();
}
Current_Attendance_Label.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
e.Handled = true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
else
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
returnValue = textBox1.Text.Replace(@"*", "");
if (e.KeyChar == "*") return;
{
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
//listBox1.Items.RemoveAt(n);
}
}
}
else
listBox1.Items.Add(returnValue);
textBox1.Clear();
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName);
foreach (object item in listBox1.Items)
SaveFile.WriteLine(item.ToString());
SaveFile.Flush();
SaveFile.Close();
if (listBox1.Items.Count != 0) { DisableCloseButton(); }
else
{
EnableCloseButton();
}
Current_Attendance_Label.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
e.Handled = true;
}
}
}
解决方案
Use the character. Single quotes '
define a character
, while double quotes "
define a string
:
if (e.KeyChar == 'L')
这篇关于运算符'=='不能应用于类型为“字符”和“串”的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文