Jump to content

Hi guys, I have a warning with this code:

		    if (comboBox2.SelectedValue == Cash)
            {
                comboBox3.Enabled = false;
            }
            else
            {
                comboBox3.Enabled = true;
            }

it says in the warning that Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string' .

I hope you can help me. Thnks :)

Link to comment
https://linustechtips.com/topic/667378-warning-code/
Share on other sites

Link to post
Share on other sites

Oh, I think I see now: I think it's saying that 

comboBox2.SelectedValue

is a pointer reference, and you need to dereference it (like by casting it to a string)  if you want to check its value rather than check that it points to a particular memory address.

 

I could be wrong. I am Bad At Pointers.

Link to comment
https://linustechtips.com/topic/667378-warning-code/#findComment-8613088
Share on other sites

Link to post
Share on other sites

6 hours ago, Factory Factory said:

Oh, I think I see now: I think it's saying that 


comboBox2.SelectedValue

is a pointer reference, and you need to dereference it (like by casting it to a string)  if you want to check its value rather than check that it points to a particular memory address.

 

I could be wrong. I am Bad At Pointers.

comboBox2.SelectedItem.ToString() == "Cash"

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/667378-warning-code/#findComment-8614020
Share on other sites

Link to post
Share on other sites

You are getting the warning because ListControl.SelectedValue (inherited by ComboBox) returns an object. When == is used on an expression of type object it will resolve to Object.ReferenceEquals.

 

The implementation provided by @vorticalbox is correct.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/667378-warning-code/#findComment-8615679
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×