473,473 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is this a bug?

Can someone confirm whether the following technique is now broken in .NET
2.0 (it worked in 1.1).

// Avoid boxing and losing our return value
object inoutCancel = false;

Control.Invoke(..., new object[] { ..., inoutCancel});
bool cancel = (bool)inoutCancel;

"inoutCancel" is always returned as "false". Any feedback would be
appreciated. Thanks.
Sep 19 '06 #1
7 1658
Can someone confirm whether the following technique is now broken in .NET
2.0 (it worked in 1.1).

// Avoid boxing and losing our return value
object inoutCancel = false;

Control.Invoke(..., new object[] { ..., inoutCancel});
bool cancel = (bool)inoutCancel;

"inoutCancel" is always returned as "false". Any feedback would be
appreciated. Thanks.
Sorry, if I didn't make it clear, "inoutCancel" is an "out" parameter.
Sep 19 '06 #2
John Brown <no_spam@_nospam.comwrote:
Can someone confirm whether the following technique is now broken in .NET
2.0 (it worked in 1.1).

// Avoid boxing and losing our return value
object inoutCancel = false;
That's not avoiding boxing at all - it's just boxing it earlier!
Control.Invoke(..., new object[] { ..., inoutCancel});
bool cancel = (bool)inoutCancel;

"inoutCancel" is always returned as "false". Any feedback would be
appreciated. Thanks.
In 1.1, when an out/ref parameter which was originally a boxed value
type changes, the value in the box changes. In 2.0, the element in the
array is set to a new boxed value. Here's a small app to demonstrate
this:

using System;
using System.Reflection;

class Test
{
public static void SetToFive (out int x)
{
x = 5;
}

static void Main()
{
MethodInfo mi = typeof(Test).GetMethod("SetToFive");

object parameter = 0;
object[] parameters = new object[]{parameter};
mi.Invoke (null, parameters);

Console.WriteLine ("After invocation:");
Console.WriteLine ("parameter={0}", parameter);
Console.WriteLine ("parameters[0]={0}", parameters[0]);
}
}

On .NET 1.1, the output is:
After invocation:
parameter=5
parameters[0]=5

On .NET 2.0, the output is:
After invocation:
parameter=0
parameters[0]=5

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 19 '06 #3
John Brown <no_spam@_nospam.comwrote:
>Can someone confirm whether the following technique is now broken in .NET
2.0 (it worked in 1.1).

// Avoid boxing and losing our return value
object inoutCancel = false;

That's not avoiding boxing at all - it's just boxing it earlier!
Thanks the feedback. In actuality however, that was a quote from Chris Sells
who's probably more widely known in C++ circles (which is my own
background). You can read what he said here if you're interested (just
search for "avoid boxing" and see the paragraph that immediately follows

http://msdn.microsoft.com/library/en...asp?frame=true
In 1.1, when an out/ref parameter which was originally a boxed value
type changes, the value in the box changes. In 2.0, the element in the
array is set to a new boxed value. Here's a small app to demonstrate
this:

using System;
using System.Reflection;

class Test
{
public static void SetToFive (out int x)
{
x = 5;
}

static void Main()
{
MethodInfo mi = typeof(Test).GetMethod("SetToFive");

object parameter = 0;
object[] parameters = new object[]{parameter};
mi.Invoke (null, parameters);

Console.WriteLine ("After invocation:");
Console.WriteLine ("parameter={0}", parameter);
Console.WriteLine ("parameters[0]={0}", parameters[0]);
}
}

On .NET 1.1, the output is:
After invocation:
parameter=5
parameters[0]=5

On .NET 2.0, the output is:
After invocation:
parameter=0
parameters[0]=5
This change breaks existing code however. Maybe the original behaviour was a
problem and had to be corrected for some reason but at first glance this
change seems totally absurd and counterintuitive. The array contains a
reference to an object which is being passed as an "out" parameter. That
object should therefore be changed and it's potentially dangerous to do
anything else (or potentially error prone anyway since the programmer may be
storing the original reference somewhere which will no longer point to the
updated value - any such references must therefore be updated after each
call) . More importantly perhaps, how do they justify replacing the original
array element with another. It's syntactically inconvenient as well since
you can no longer create the array on the fly (in the function call) because
you now have to keep it around in order to index into it afterwards. Even
worse, you have to hardcode the index of the parameter itself or otherwise
store it somewhere so you can index into the array afterwards (not only
ugly, but the index will have to be changed if the parameter's ordinal
position ever changes). This is all brutal IMO and I'd like to know what the
justification was (assuming it wasn't a mistake - they must have had a
legitimate reason for it otherwise). Anyway, thanks again.
Sep 20 '06 #4
John Brown <no_spam@_nospam.comwrote:
John Brown <no_spam@_nospam.comwrote:
Can someone confirm whether the following technique is now broken in .NET
2.0 (it worked in 1.1).

// Avoid boxing and losing our return value
object inoutCancel = false;
That's not avoiding boxing at all - it's just boxing it earlier!

Thanks the feedback. In actuality however, that was a quote from Chris Sells
who's probably more widely known in C++ circles (which is my own
background). You can read what he said here if you're interested (just
search for "avoid boxing" and see the paragraph that immediately follows

http://msdn.microsoft.com/library/en...asp?frame=true
I'm surprised to see Mr Sells making such a comment. I suspect I know
what he means, but it's really badly worded. (It's definitely not
avoiding boxing - it's just doing it at a different point.)
This change breaks existing code however.
Indeed. I don't know why it's been changed, and I can see that relying
on the ordinal is a bit of a pain. On the other hand, I don't think it
was ever *documented* to behave in the way it did previously, so
arguably MS was free to change the behaviour. Anyway, at least you've
got a workaround now.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #5
I'm surprised to see Mr Sells making such a comment. I suspect I know
what he means, but it's really badly worded. (It's definitely not
avoiding boxing - it's just doing it at a different point.)
Actually I don't see it that way at all since [un]boxing is clearly going on
and he must surely know it (I mean, it's plainly obvious). His statement
simply reflects the timing of it. If he assigns to a "bool" directly and
then tries to stuff that into the array, a boxed value will end up in the
array instead of the original bool. So the original bool will never be
updated. I think it's pretty clear then that his use of the term "avoid"
refers to the latter situation, i.e., the "boxing" he refers to is that
which would otherwise occur if he didn't "avoid" it from the get-go (which
his comment therefore refers to notwithstanding his placement of it - right
above the assignment itself which is arguably appropriate given it's an
"object" and not a "bool" for the very reason he cites - his comment is
nevertheless a reflection of the entire situation in general).
>
>This change breaks existing code however.

Indeed. I don't know why it's been changed, and I can see that relying
on the ordinal is a bit of a pain. On the other hand, I don't think it
was ever *documented* to behave in the way it did previously, so
arguably MS was free to change the behaviour.
Assuming however that the C# language lawyers didn't make this change for
some pressing reason, on the surface it appears broken. My instincts also
tell me that it might violate the C# standard but I'd have to investigate
further (just a "gut" feeling only for now)
Anyway, at least you've got a workaround now.
Yes, thanks again for your assistance (appreciated).
Sep 21 '06 #6
John Brown wrote:
I'm surprised to see Mr Sells making such a comment. I suspect I know
what he means, but it's really badly worded. (It's definitely not
avoiding boxing - it's just doing it at a different point.)

Actually I don't see it that way at all since [un]boxing is clearly going on
and he must surely know it (I mean, it's plainly obvious). His statement
simply reflects the timing of it. If he assigns to a "bool" directly and
then tries to stuff that into the array, a boxed value will end up in the
array instead of the original bool. So the original bool will never be
updated. I think it's pretty clear then that his use of the term "avoid"
refers to the latter situation, i.e., the "boxing" he refers to is that
which would otherwise occur if he didn't "avoid" it from the get-go (which
his comment therefore refers to notwithstanding his placement of it - right
above the assignment itself which is arguably appropriate given it's an
"object" and not a "bool" for the very reason he cites - his comment is
nevertheless a reflection of the entire situation in general).
It's clear to both of us that the statement below the comment does
boxing, but suppose you weren't confident in your understanding of
boxing - wouldn't you get nervous from such a comment? It could at the
very least be better worded, eg:

// Do boxing up front, so we can keep a reference to the box
This change breaks existing code however.
Indeed. I don't know why it's been changed, and I can see that relying
on the ordinal is a bit of a pain. On the other hand, I don't think it
was ever *documented* to behave in the way it did previously, so
arguably MS was free to change the behaviour.

Assuming however that the C# language lawyers didn't make this change for
some pressing reason, on the surface it appears broken. My instincts also
tell me that it might violate the C# standard but I'd have to investigate
further (just a "gut" feeling only for now)
This is absolutely *not* part of the C# standard. It's not a language
issue at all - it's an issue in terms of the implementation of
reflection.
Anyway, at least you've got a workaround now.

Yes, thanks again for your assistance (appreciated).
No problem.

Jon

Sep 21 '06 #7
It's clear to both of us that the statement below the comment does
boxing, but suppose you weren't confident in your understanding of
boxing - wouldn't you get nervous from such a comment? It could at the
very least be better worded, eg:

// Do boxing up front, so we can keep a reference to the box
Agreed but it's really a religious issue. Given his explanation that follows
coupled with the fact that the article itself isn't about boxing (it's just
a side issue), I'm cutting him some slack. I doubt he was analyzing the
issue ad-nauseam at the time :)
This is absolutely *not* part of the C# standard. It's not a language
issue at all - it's an issue in terms of the implementation of
reflection.
I'll take your word for it :) since it was only a gut feeling based on many
years in the C/C++ trenches (drawing precedent where perhaps I shouldn't)
Sep 21 '06 #8

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
jinu1996
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
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...

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.