473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

== operator not working on boxed value types

I have the following routine that is always throwing the error at the end.
It never returns from within the foreach loop because the if expression is
always evaluating to false apparently. This is something of a problem. The
comparison is between two objects (Handler.Data is defined as returning an
object.), but these objects are always nothing more than boxed value types
(either strings or ints or an int based enumeration). If for the sake of a
particular test where I knew all the values were ints, I recoded the
comparison to look like
if((int)Handler.Data == (int)Value)
and all worked fine. The problem is I can rely on this assumption. Any
suggestions on this issue?

protected virtual void HandleValueChange(ref object Value)
{
foreach(PSCForms.MenuHandler Handler in MenuHandlers)
if(Handler.Data == Value)
{
Handler.MenuItem.DefaultItem = true;
this.Text = Handler.MenuItem.Text.Replace("&", "");
return;
}

throw new ArgumentOutOfRangeException(
"Value", Value, "The value is not is the list of acceptable values.");
}

P.S. One more bizarre thing, if I put (Handler.Data == Value) in a watch
window and step through the routine, the window indicates that the
expression evaluates to true for the appropriate iteration, but the code
within the if block still does not run. Could this be a C# bug?

--Ken
Nov 15 '05 #1
6 1962
Hello

With the variables of type object, the == performs a reference equality,
which is usually false unless the 2 objects actually reference to the same
object.
You should use the static method Object.Equals(object1, object2).

Best regards
Sherif

"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:us**************@TK2MSFTNGP09.phx.gbl...
I have the following routine that is always throwing the error at the end.
It never returns from within the foreach loop because the if expression is
always evaluating to false apparently. This is something of a problem. The comparison is between two objects (Handler.Data is defined as returning an
object.), but these objects are always nothing more than boxed value types
(either strings or ints or an int based enumeration). If for the sake of a particular test where I knew all the values were ints, I recoded the
comparison to look like
if((int)Handler.Data == (int)Value)
and all worked fine. The problem is I can rely on this assumption. Any
suggestions on this issue?

protected virtual void HandleValueChange(ref object Value)
{
foreach(PSCForms.MenuHandler Handler in MenuHandlers)
if(Handler.Data == Value)
{
Handler.MenuItem.DefaultItem = true;
this.Text = Handler.MenuItem.Text.Replace("&", "");
return;
}

throw new ArgumentOutOfRangeException(
"Value", Value, "The value is not is the list of acceptable values.");
}

P.S. One more bizarre thing, if I put (Handler.Data == Value) in a watch
window and step through the routine, the window indicates that the
expression evaluates to true for the appropriate iteration, but the code
within the if block still does not run. Could this be a C# bug?

--Ken

Nov 15 '05 #2
I am aware of how the operator works for object variables in general but I
was under the impression it would unbox boxed value types before applying a
operator. This is particularly so since the watch window did in fact return
the value I was expecting. That the watch window and code execution are out
of step is most disturbing.

What about the instance method Equals()? I just tested it and it works as
well. This is unrelated to my issue but why is there both an instance and
static method?

--Ken

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...
Hello

With the variables of type object, the == performs a reference equality,
which is usually false unless the 2 objects actually reference to the same
object.
You should use the static method Object.Equals(object1, object2).

Best regards
Sherif

"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:us**************@TK2MSFTNGP09.phx.gbl...
I have the following routine that is always throwing the error at the end. It never returns from within the foreach loop because the if expression is always evaluating to false apparently. This is something of a problem. The
comparison is between two objects (Handler.Data is defined as returning an object.), but these objects are always nothing more than boxed value types (either strings or ints or an int based enumeration). If for the sake of a
particular test where I knew all the values were ints, I recoded the
comparison to look like
if((int)Handler.Data == (int)Value)
and all worked fine. The problem is I can rely on this assumption. Any
suggestions on this issue?

protected virtual void HandleValueChange(ref object Value)
{
foreach(PSCForms.MenuHandler Handler in MenuHandlers)
if(Handler.Data == Value)
{
Handler.MenuItem.DefaultItem = true;
this.Text = Handler.MenuItem.Text.Replace("&", "");
return;
}

throw new ArgumentOutOfRangeException(
"Value", Value, "The value is not is the list of acceptable

values."); }

P.S. One more bizarre thing, if I put (Handler.Data == Value) in a watch
window and step through the routine, the window indicates that the
expression evaluates to true for the appropriate iteration, but the code
within the if block still does not run. Could this be a C# bug?

--Ken


Nov 15 '05 #3
Kenneth Baltrinic <ke*****@baltrinic.com> wrote:
I have the following routine that is always throwing the error at the end.
<snip>
protected virtual void HandleValueChange(ref object Value)
Sherif has answered your main question, but I'm concerned with your use
of "ref" here - you're not changing the value of the "Value" variable,
which means that it being passed by reference is irrelevant. Is there
more code in the real method which makes it worth doing this?

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
P.S. One more bizarre thing, if I put (Handler.Data == Value) in a watch
window and step through the routine, the window indicates that the
expression evaluates to true for the appropriate iteration, but the code
within the if block still does not run. Could this be a C# bug?


I rather suspect that the watch window uses slightly different
techniques to evaluate things, unfortunately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Kenneth Baltrinic <ke*****@baltrinic.com> wrote:
I am aware of how the operator works for object variables in general but I
was under the impression it would unbox boxed value types before applying a
operator.
Why would it do that? It would have to check whether or not the
reference type was a boxed value, and make that check *every* time it
did a reference comparison. That would make it *much* slower, IMO.
What about the instance method Equals()? I just tested it and it works as
well. This is unrelated to my issue but why is there both an instance and
static method?


The static method is more convenient if the references in question
could both be null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Hello

The instance version of Equals is fine, but object1.Equals(object2) will
throw a NullReferenceException if object1 is null. So if you know that
object1 should never be null then instance version is fine ( and even
better). The static version check for if the value is null, and actually
calls the instance version.

Best regards,
Sherif

"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
I am aware of how the operator works for object variables in general but I
was under the impression it would unbox boxed value types before applying a operator. This is particularly so since the watch window did in fact return the value I was expecting. That the watch window and code execution are out of step is most disturbing.

What about the instance method Equals()? I just tested it and it works as
well. This is unrelated to my issue but why is there both an instance and
static method?

--Ken

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...
Hello

With the variables of type object, the == performs a reference equality,
which is usually false unless the 2 objects actually reference to the same
object.
You should use the static method Object.Equals(object1, object2).

Best regards
Sherif

"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:us**************@TK2MSFTNGP09.phx.gbl...
I have the following routine that is always throwing the error at the end. It never returns from within the foreach loop because the if expression
is
always evaluating to false apparently. This is something of a
problem. The
comparison is between two objects (Handler.Data is defined as
returning an object.), but these objects are always nothing more than boxed value types (either strings or ints or an int based enumeration). If for the sake
of
a
particular test where I knew all the values were ints, I recoded the
comparison to look like
if((int)Handler.Data == (int)Value)
and all worked fine. The problem is I can rely on this assumption.

Any suggestions on this issue?

protected virtual void HandleValueChange(ref object Value)
{
foreach(PSCForms.MenuHandler Handler in MenuHandlers)
if(Handler.Data == Value)
{
Handler.MenuItem.DefaultItem = true;
this.Text = Handler.MenuItem.Text.Replace("&", "");
return;
}

throw new ArgumentOutOfRangeException(
"Value", Value, "The value is not is the list of acceptable

values."); }

P.S. One more bizarre thing, if I put (Handler.Data == Value) in a watch window and step through the routine, the window indicates that the
expression evaluates to true for the appropriate iteration, but the code within the if block still does not run. Could this be a C# bug?

--Ken



Nov 15 '05 #6
Good point about the null exception. Its a non issue in this case. And I
do see the point about why it does not unbox. For some reason I was
thinking that the decision to unbox could be made at compile time. Duh.
Fuzzy thinking yesterday.

Regarding the person who queried about the use of the ref modifier, this is
a protected implementation. Overriding implementations may need to modify
the value though this one does not.

Again thanks for al the help.

--Ken
"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:us****************@TK2MSFTNGP09.phx.gbl...
I have the following routine that is always throwing the error at the end.
It never returns from within the foreach loop because the if expression is
always evaluating to false apparently. This is something of a problem. The comparison is between two objects (Handler.Data is defined as returning an
object.), but these objects are always nothing more than boxed value types
(either strings or ints or an int based enumeration). If for the sake of a particular test where I knew all the values were ints, I recoded the
comparison to look like
if((int)Handler.Data == (int)Value)
and all worked fine. The problem is I can rely on this assumption. Any
suggestions on this issue?

protected virtual void HandleValueChange(ref object Value)
{
foreach(PSCForms.MenuHandler Handler in MenuHandlers)
if(Handler.Data == Value)
{
Handler.MenuItem.DefaultItem = true;
this.Text = Handler.MenuItem.Text.Replace("&", "");
return;
}

throw new ArgumentOutOfRangeException(
"Value", Value, "The value is not is the list of acceptable values.");
}

P.S. One more bizarre thing, if I put (Handler.Data == Value) in a watch
window and step through the routine, the window indicates that the
expression evaluates to true for the appropriate iteration, but the code
within the if block still does not run. Could this be a C# bug?

--Ken

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
1
by: Andrew Larder | last post by:
Hi, I'm trying to write a high performance numerical app in C#, and would like to use operator overloading, but currently things are running a bit slower than non op-overloaded code. If I...
3
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
10
by: Andrew Robinson | last post by:
I don't know if this is a compiler error or not. It all kind of makes sense but you would think that the compiler would check the types of the possible outcomes of the tertiary operator (c and d)...
15
by: James | last post by:
Which is better, which is faster, which is easier etc... ????? String.Format ( "yadda {0} yadda {1}", x, y ) "yadda" + x.ToString() + " yadda" + y.tostring(); My code has a mish mash of...
2
by: AshokG | last post by:
Hi, In search of the differences between struct and class I found this MSDN Doc on Boxed Value Types: http://msdn2.microsoft.com/en-us/library/f08s4k28(VS.90).aspx The output is specified...
15
by: PengYu.UT | last post by:
Hi, It seems that C++ does not allow overloading operators for primative types, e.g. int, double. I'm wondering whether it is ture or there is some walk-around? Thanks, Peng #include...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.