我有这个,蚂蚁一切都好,但它说“Posible意外的参考比较;得到一个值比较左侧输入'string' [英] I have this, ant it's all ok, but it says "Posible unintended reference comparison; to get a value comparison cast left hand side to type 'string'

查看:101
本文介绍了我有这个,蚂蚁一切都好,但它说“Posible意外的参考比较;得到一个值比较左侧输入'string'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if ((Convert.ToInt16(txtID.Text)==  1 && cboCategoria.SelectedItem ==   Alimentos)|| Convert.ToInt16( txtID.Text)==  6 && cboCategoria.SelectedItem ==   Alimentos))

解决方案

SelectedItem是一个对象,整个项目,你真的想要SelectedText属性或SelectedValue属性。所以请改用comboBox.SelectedText。



http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem(v = vs.110)的.aspx [ ^ ]


我会添加正确的解决方案1:这是一种糟糕的编程风格;将UI状态中的某些内容与代码中的一些立即常量进行比较的整个想法是错误的想法和错误的代码设计。



这样的立即常数为Alimentos被称为魔术字符串。这样的编程是不可维护的,如果你写两次相同的常量,这就变得特别明显。如果拼错任何这些字符串,编译器将不会检测到任何问题。这是众所周知的反模式。请参阅: http://en.wikipedia.org/wiki/Magic_string_%28programming%29#Magic_strings_in_code [ ^ ]。



另请参阅: http://en.wikipedia.org/wiki/反模式 [ ^ ]。



怎么办?在最坏的情况下,至少声明一个显式常量,并在整个解决方案中只执行一次。它应该是声明字符串的唯一位置。这些是基本原则:

http://en.wikipedia.org/wiki/Single_Point_of_Truth [ ^ ],

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ]。



即使你这样做它,这种方法看起来太过特别。正确的方法就是这样的。 不要在列表框,组合框和其他项控件中放置字符串。创建表示功能所需数据的特殊类型(类或结构),并将它们的实例添加到项控件中。然后,例如,为了进行比较,将项目输入到您的类型并使用您的项目类型的成员。



剩下的问题是:什么是显示在用户界面?简单:在最简单的情况下,无论其方法 ToString()显示,所以你可以覆盖 System.Object.ToString()。或者,更好的是,使用绑定,这是一个完全独立的主题。



-SA

if ((Convert.ToInt16(txtID.Text) == 1 && cboCategoria.SelectedItem== "Alimentos") || Convert.ToInt16(txtID.Text)== 6 && cboCategoria.SelectedItem == "Alimentos"))

解决方案

The SelectedItem is an object, the whole item, you really want the SelectedText property or the SelectedValue property. So use comboBox.SelectedText instead.

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem(v=vs.110).aspx[^]


I would add to the correct Solution 1: this is a bad style of programming; the whole idea of comparison of something in the UI state with some immediate constants in your code is bad idea and wrong code design.

Such immediate constants as "Alimentos" are referred to as "magic strings". Such programming is not maintainable, which becomes especially obvious if you write the same constant twice. The compiler won't detect any problem is you misspell any of those strings. This is a well-known anti-pattern. Please see: http://en.wikipedia.org/wiki/Magic_string_%28programming%29#Magic_strings_in_code[^].

See also: http://en.wikipedia.org/wiki/Anti-pattern[^].

What to do? In worst case, at least declare an explicit constant and do it only once in your whole solution. It should be the only place where the string is declared. These are the fundamental principles:
http://en.wikipedia.org/wiki/Single_Point_of_Truth[^],
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

But even you do it, this approach looks way too much ad-hoc. Right approach would be something like this. Don't put strings in your list boxes, combo boxes and other item controls. Create special types (classes or structures) representing the data you need for your functionality, and add instances of them to your item controls. Then, for example, for comparison, type-cast the items to your types and use your item type's members.

The remaining problem is: what is to be shown in the UI? Simple: in simplest case, whatever its method ToString() shows, so you can override System.Object.ToString(). Or, better, use binding, which is a whole big separate topic.

—SA


这篇关于我有这个,蚂蚁一切都好,但它说“Posible意外的参考比较;得到一个值比较左侧输入'string'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆