如何更正“可能的非预期参考比较”? [英] How can I correct the "possible unintended reference comparison"?

查看:76
本文介绍了如何更正“可能的非预期参考比较”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码我收到警告



可能的非预期参考比较;要获得值比较,请将右侧强制转换为'string'< br $>


但FullName和按钮Tag都是字符串。

With the following code I get a warning

"Possible unintended reference comparison;to get a value comparison, cast the right side to type 'string'"

But both FullName and the button Tag are both strings.

private void _RemoveParty(object obj)
        {
            ContactListTable.Remove(ContactListTable.First(u => u.FullName == ((obj as Button).Tag)));
            if (ContactListTable.Count() <= 0)
            {
                ContactListTable.Add(new ContactData());
            }
            
        }





我尝试过:



我试过添加ToString()并改变我的演员但无济于事。



What I have tried:

I have tried adding ToString() and changing my casting but to no avail.

推荐答案

't问题是Tag属性 不是 一个字符串 - 它是一个对象

该对象可能包含一个字符串,但它同样可以包含Banana,TextBox或整数 - 编译器不知道,并且可以找出直到运行时间。

所以比较是在一个字符串和一个对象之间 - 它只能是一个参考比较,而不是普通字符串到字符串版本,这是一个值比较。差别很大:作为文字常量的字符串hello world与用户在文本框中输入的相同字符串hello world不同。当你进行值比较时它们是相同的,因为每个单独的字符是相同的,但它们与参考比较不同,因为它们不存储在堆上的相同位置。



修复它很简单:

'tThe problem is that the Tag property isn't a string - it's an object.
That object may contain a string, but it could equally contain a Banana, or a TextBox, or an integer - the compiler doesn't know, and can;t find out until run time.
So the comparison is between a string and an object - which can only ever be a reference comparison, instead of the "normal" string to string version which is a value comparison. And the difference is significant: the string "hello world" as a literal constant is not the same reference as the same string "hello world" entered by a user in a textbox. They are the same when you do a value comparison because each individual character is the same, but they are not the same as a reference comparison because they are not stored in the same place on the heap.

To fix it is simple:
Button b = obj as Button;
if (b != null)
   {
   string s = (string)(b.Tag);
   ContactListTable.Remove(ContactListTable.First(u => u.FullName == s));
   }


首先,WPF不需要像 System.Windows.Forms那样的标签。 / code>。



你错了很明显:==运算符的左边是字符串,而写的是 System.Object



该怎么办?这将是一个错误的问题。这取决于您存储为按钮标签的实际对象和目的。如果您需要帮助,请先解释您想要实现的目标。但是识别按钮的整个想法是错误的。



是的,有时你可以使用元素的标签,但是你不需要识别任何东西。例如,标记可以是处理程序应该使用的某个对象,类似的东西。此外,建议使用WPF命令系统。



-SA
First of all, WPF does not require tags as much as, say, System.Windows.Forms.

You mistake is quite obvious: left of == operator is string, and write is System.Object.

"What to do?" would be a wrong question. It depends on the actual object you stored as the button's tag and your purpose. If you need help, start with explaining what you wanted to achieve with that. But the whole idea to identify the button is wrong.

Yes, sometimes you can use element's Tag, but you don't need to identify anything. For example, the tag can be some object the handler is supposed to work with, something like that. Also, it's advisable to use WPF command system.

—SA


使用



Using

ContactListTable.Remove(ContactListTable.First(u => u.FullName == (obj as Button).Tag as string));





删除警告并保留.Tag的值,即使它为空。使用.ToString()可以进行比较,但如果按钮Tag为null则抛出错误。



Removes the warning and preserves the value of .Tag even if it's null. Using .ToString() allows you to make the comparison but throwes an error if the button Tag is null.


这篇关于如何更正“可能的非预期参考比较”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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