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 HandleValueChan ge(ref object Value)
{
foreach(PSCForm s.MenuHandler Handler in MenuHandlers)
if(Handler.Data == Value)
{
Handler.MenuIte m.DefaultItem = true;
this.Text = Handler.MenuIte m.Text.Replace( "&", "");
return;
}
throw new ArgumentOutOfRa ngeException(
"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 6 1976
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(o bject1, object2).
Best regards
Sherif
"Kenneth Baltrinic" <ke*****@baltri nic.com> wrote in message
news:us******** ******@TK2MSFTN GP09.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 HandleValueChan ge(ref object Value) { foreach(PSCForm s.MenuHandler Handler in MenuHandlers) if(Handler.Data == Value) { Handler.MenuIte m.DefaultItem = true; this.Text = Handler.MenuIte m.Text.Replace( "&", ""); return; }
throw new ArgumentOutOfRa ngeException( "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
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.NO SPAM> wrote in message
news:e%******** ********@TK2MSF TNGP09.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(o bject1, object2).
Best regards Sherif
"Kenneth Baltrinic" <ke*****@baltri nic.com> wrote in message news:us******** ******@TK2MSFTN GP09.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 HandleValueChan ge(ref object Value) { foreach(PSCForm s.MenuHandler Handler in MenuHandlers) if(Handler.Data == Value) { Handler.MenuIte m.DefaultItem = true; this.Text = Handler.MenuIte m.Text.Replace( "&", ""); return; }
throw new ArgumentOutOfRa ngeException( "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
Kenneth Baltrinic <ke*****@baltri nic.com> wrote: I have the following routine that is always throwing the error at the end.
<snip>
protected virtual void HandleValueChan ge(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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Kenneth Baltrinic <ke*****@baltri nic.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hello
The instance version of Equals is fine, but object1.Equals( object2) will
throw a NullReferenceEx ception 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*****@baltri nic.com> wrote in message
news:OE******** ******@TK2MSFTN GP09.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.NO SPAM> wrote in message news:e%******** ********@TK2MSF TNGP09.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(o bject1, object2).
Best regards Sherif
"Kenneth Baltrinic" <ke*****@baltri nic.com> wrote in message news:us******** ******@TK2MSFTN GP09.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 HandleValueChan ge(ref object Value) { foreach(PSCForm s.MenuHandler Handler in MenuHandlers) if(Handler.Data == Value) { Handler.MenuIte m.DefaultItem = true; this.Text = Handler.MenuIte m.Text.Replace( "&", ""); return; }
throw new ArgumentOutOfRa ngeException( "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
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*****@baltri nic.com> wrote in message
news:us******** ********@TK2MSF TNGP09.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 HandleValueChan ge(ref object Value) { foreach(PSCForm s.MenuHandler Handler in MenuHandlers) if(Handler.Data == Value) { Handler.MenuIte m.DefaultItem = true; this.Text = Handler.MenuIte m.Text.Replace( "&", ""); return; }
throw new ArgumentOutOfRa ngeException( "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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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, but I would like to use
internally my own = operator for some of my value types, so I can say "x =
y;" rather than "x.Assign(y);".
The op_Assign operator seems impossibly broken since it takes __value copies
of the two objects. Perhaps there is...
|
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 overload operators on a struct, lets for example say a Vector3 (3D
vector), does this mean when I write:
Vector3 a = new Vector(1,2,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
all(?) the conversion routines necessary to convert from Int32 and string.
(see the listing at the end)
When I cast directly from a value of Int32 type, it works:
|
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 syntax.
IIf works in most cases, but in some instances you want to use expressions
which may fail if they are evaluated when they arent supposed to be, and it
would be nice to have a concise way of writing this instead of using a whole
If-Then-Else...
|
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) against the type of the variable
being assigned (a). Instead it compares the types of c and d which may not
be compatible. Just seems silly.
int? i;
object o;
//this is ok:
| |
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 both and I am wondewring if I should standardize
or not ??
|
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 as this if you run this code
----------
////// doing this with a value type
|
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 <iostream>
|
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;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |