473,385 Members | 1,944 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,385 software developers and data experts.

(un)boxing

I have a Hashtable of ints keyed on Guids, and I want to do the following:

foreach ( DataRow row in dsWorkDays.Tables[0].Rows )

{

Guid PersonId = (Guid)row["PersonID"];

DateTime Day = (DateTime)row["Datum"];

rLastWorkDays.Increment( PersonId, Day.DayOfWeek,
++((int)Offsets[PersonId]) );

}

that is, increment the offset for the current Guid and call a function with
the new value.

This fails at compile time with the message

The left-hand side of an assignment must be a variable, property or
indexer

I assume that the rule is that when you access a hashed value type, you
don't get a reference but a copy. When you assign the copy, everything is
OK, but when, like here, you just try and perform an operation on it, the
compiler recognises that the operation will always be useless because it's
performed on a temporary and refuses to comply.

If this is right, my question is: why isn't boxing automatically invoked? I
can do it myself of course, but then I have to write about the same amount
of code as I would just to store the value, increment it, call the function,
and tuck the incremented value back in the Hashtable. Any clarifications?
Perhaps I've misunderstood the rule.

Alistair Welchman

Nov 15 '05 #1
4 1696
I don't see how (un)boxing would help you anyway. The point is the Hashtable
is returning you a copy of the value when you call 'Offsets[PersonId]) ' and
consequently you have to place it back into the Hashtable for the value to
be updated:

Offsets[PersonId] = ++((int)Offsets[PersonId]);

Regards
Lee
"Alistair Welchman" <th*******@earthlink.net> wrote in message
news:OL*************@tk2msftngp13.phx.gbl...
I have a Hashtable of ints keyed on Guids, and I want to do the following:

foreach ( DataRow row in dsWorkDays.Tables[0].Rows )

{

Guid PersonId = (Guid)row["PersonID"];

DateTime Day = (DateTime)row["Datum"];

rLastWorkDays.Increment( PersonId, Day.DayOfWeek,
++((int)Offsets[PersonId]) );

}

that is, increment the offset for the current Guid and call a function with the new value.

This fails at compile time with the message

The left-hand side of an assignment must be a variable, property or
indexer

I assume that the rule is that when you access a hashed value type, you
don't get a reference but a copy. When you assign the copy, everything is
OK, but when, like here, you just try and perform an operation on it, the
compiler recognises that the operation will always be useless because it's
performed on a temporary and refuses to comply.

If this is right, my question is: why isn't boxing automatically invoked? I can do it myself of course, but then I have to write about the same amount
of code as I would just to store the value, increment it, call the function, and tuck the incremented value back in the Hashtable. Any clarifications?
Perhaps I've misunderstood the rule.

Alistair Welchman

Nov 15 '05 #2
You only get a copy back from a Hashtable when you've stored a value type,
otherwise you get a reference.

But you're right that boxing won't help, because of the way operator
overloading is done in C#; that is always with static functions. This is
unlike C++ where you have the choice between member and non-member
functions. My thought was: if operator++ could be overloaded using a member
function, I would expect it to behave like the member function SomeRefType
Inc() in this snippet:

DoComputation( ((SomeRefType)myHash[myKey]).Inc() );

And then a suitably defined boxed version of a value type would handle my
situation.

Anyway, your code suggestion doesn't compile either:

Offsets[PersonId] = ++((int)Offsets[PersonId]); // same error as before

I'm now more confused than I was at first, since this can't have to do with
the creation of a temporary that's never assigned storage.

What I'm doing is this:

int Offset = (int)Offsets[PersonId];
rLastWorkDays.Increment( PersonId, Day.DayOfWeek, ++Offset );
Offsets[PersonId] = Offset;

which, while hardly the end of the world, is not as clear and conscise as it
could have been.

Any *more* clarifications?

Alistair

"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:e9**************@tk2msftngp13.phx.gbl...
I don't see how (un)boxing would help you anyway. The point is the Hashtable is returning you a copy of the value when you call 'Offsets[PersonId]) ' and consequently you have to place it back into the Hashtable for the value to
be updated:

Offsets[PersonId] = ++((int)Offsets[PersonId]);

Regards
Lee
"Alistair Welchman" <th*******@earthlink.net> wrote in message
news:OL*************@tk2msftngp13.phx.gbl...
I have a Hashtable of ints keyed on Guids, and I want to do the following:
foreach ( DataRow row in dsWorkDays.Tables[0].Rows )

{

Guid PersonId = (Guid)row["PersonID"];

DateTime Day = (DateTime)row["Datum"];

rLastWorkDays.Increment( PersonId, Day.DayOfWeek,
++((int)Offsets[PersonId]) );

}

that is, increment the offset for the current Guid and call a function with
the new value.

This fails at compile time with the message

The left-hand side of an assignment must be a variable, property or
indexer

I assume that the rule is that when you access a hashed value type, you
don't get a reference but a copy. When you assign the copy, everything is OK, but when, like here, you just try and perform an operation on it, the compiler recognises that the operation will always be useless because it's performed on a temporary and refuses to comply.

If this is right, my question is: why isn't boxing automatically invoked? I
can do it myself of course, but then I have to write about the same

amount of code as I would just to store the value, increment it, call the

function,
and tuck the incremented value back in the Hashtable. Any clarifications? Perhaps I've misunderstood the rule.

Alistair Welchman


Nov 15 '05 #3
> Anyway, your code suggestion doesn't compile either:

Offsets[PersonId] = ++((int)Offsets[PersonId]); // same error as before
I'm now more confused than I was at first, since this can't have to do with the creation of a temporary that's never assigned storage.


It has to do with the '++' operator. This operator actually changes the
memory location in question. Since the location returned by
Offsets[PersonId] is a temporary location, using the increment operator on
it is useless. The fact that '++' returns the new value is a side-effect. So
while your code seems right, it would be much clearer to use

Offsets[id] += 1

or, if that doesn't work,

Offsets[id] = (int)Offsets[id] + 1

The latter will compile fine, and the former should. Hmm. Maybe I'm not
really understanding what you're asking?

Chris
Nov 15 '05 #4
Alistair Welchman <th*******@earthlink.net> wrote:
Anyway, your code suggestion doesn't compile either:

Offsets[PersonId] = ++((int)Offsets[PersonId]); // same error as before

I'm now more confused than I was at first, since this can't have to do with
the creation of a temporary that's never assigned storage.

What I'm doing is this:

int Offset = (int)Offsets[PersonId];
rLastWorkDays.Increment( PersonId, Day.DayOfWeek, ++Offset );
Offsets[PersonId] = Offset;

which, while hardly the end of the world, is not as clear and conscise as it
could have been.

Any *more* clarifications?


Try:

Offsets[PersonId] = ((int)Offsets[PersonId])+1;

Note that in IL itself, you can modify the value within the box - you
just can't do it in C# because unboxing in C# always causes a copy to
be made.

--
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

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

Similar topics

1
by: Andreas Mueller | last post by:
Hi, I have a struct that implements an interface: interface IMyInterface { void DoIt(); } public struct MyStruct : IMyInterface {
11
by: Joe DeAngelo | last post by:
Is it possible to include assembler code into CSharp? If yes, could someone give me an example? Joe
5
by: Joe | last post by:
Consider the following code loop: for(int x = 0; x < 100; x++) { string sLoop = "Loop # " + (x+1).ToString(); Console.WriteLine(x); } I was told that the (x+1).ToString() was a boxing...
7
by: John Brown | last post by:
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(...,...
82
by: Peter Olcott | last post by:
I need std::vector like capability for several custom classes. I already discussed this extensively in the thread named ArrayList without Boxing and Unboxing. The solution was to simply create...
161
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my...
0
by: shamirza | last post by:
· When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and...
8
by: mrashidsaleem | last post by:
Can anyone guide me what is wrong with the naming conventions suggested below? I know that this is not recommended by many (including Microsoft) but I need to know what exactly is the rationale...
6
by: airwot4 | last post by:
I have the below code for a multithreaded winforms application. It works successfully apart from the cancel function. AppendLog2 has an out parameter which should return true if either of two...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...

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.