equals与== 的问题

wx_leaner 2016-06-04 01:25:31


为什么不行?求教解法。
源程序见下:


private void button1_Click(object sender, EventArgs e)
{
string a = Convert.ToString(myDataSet3.Tables[0].Rows[0][0]);
string b = "2";

MessageBox.Show(myDataSet3.Tables[0].Rows[0][4].GetType().ToString());
if (a==b)
allCell[0].Text = "== OK";

if (a.CompareTo(b)==0)
allCell[0].Text = "Compareto OK";

if (a.Equals(b))
allCell[0].Text = "Equals OK";

else
allCell[0].Text ="不行 "+ a.ToString() + a.ToString();

}



//test
private void button1_Click(object sender, EventArgs e)
{
string a = Convert.ToString(myDataSet3.Tables[0].Rows[0][4]);
string b = "星期五";

MessageBox.Show(myDataSet3.Tables[0].Rows[0][4].GetType().ToString());
if (a==b)
allCell[0].Text = "== OK";

if (a.CompareTo(b)==0)
allCell[0].Text = "Compareto OK";

if (a.Equals(b))
allCell[0].Text = "Equals OK";

//else
// allCell[0].Text ="不行 "+ a.ToString() + a.ToString();

}
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wx_leaner 2016-06-04
  • 打赏
  • 举报
回复
2楼我又试了一下,你说对了。确实是会产生一个空格。 可是问题是我的数据库里都是用的varchar呀,这是怎么回事?
wx_leaner 2016-06-04
  • 打赏
  • 举报
回复
回复1楼 :我是新手,除了equals其他的比较也没成功。我的这个问题有什么解法吗?

回复2楼:没有的,我的数据库设计里都是用的varchar

回复3楼:没有换行,你可以看else后面的语句。那是一个button,颜色和背景色一样,换行只是button的第一行地方不够了
Poopaye 2016-06-04
  • 打赏
  • 举报
回复
第一个星期五后面有个换行
  • 打赏
  • 举报
回复
你从table里面读出来的字符串是不是有空格或者回车符
  • 打赏
  • 举报
回复
只是给一个忠告:不十二分清楚的情况下不要使用 Equals。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧