I have a Hashtable of ints keyed on Guids, and I want to do the following:
foreach ( DataRow row in dsWorkDays.Tabl es[0].Rows )
{
Guid PersonId = (Guid)row["PersonID"];
DateTime Day = (DateTime)row["Datum"];
rLastWorkDays.I ncrement( 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 4 1714
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*******@eart hlink.net> wrote in message
news:OL******** *****@tk2msftng p13.phx.gbl... I have a Hashtable of ints keyed on Guids, and I want to do the following:
foreach ( DataRow row in dsWorkDays.Tabl es[0].Rows )
{
Guid PersonId = (Guid)row["PersonID"];
DateTime Day = (DateTime)row["Datum"];
rLastWorkDays.I ncrement( 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
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)m yHash[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.I ncrement( 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_Pl ease_Digita.com > wrote in message
news:e9******** ******@tk2msftn gp13.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*******@eart hlink.net> wrote in message news:OL******** *****@tk2msftng p13.phx.gbl... I have a Hashtable of ints keyed on Guids, and I want to do the
following: foreach ( DataRow row in dsWorkDays.Tabl es[0].Rows )
{
Guid PersonId = (Guid)row["PersonID"];
DateTime Day = (DateTime)row["Datum"];
rLastWorkDays.I ncrement( 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
> 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
Alistair Welchman <th*******@eart hlink.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.I ncrement( 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andreas Mueller |
last post by:
Hi,
I have a struct that implements an interface:
interface IMyInterface
{
void DoIt();
}
public struct MyStruct : IMyInterface
{
|
by: Joe DeAngelo |
last post by:
Is it possible to include assembler code into CSharp?
If yes, could someone give me an example?
Joe
|
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 statement, and would
therefore cause a memory leak because there's no explicit unboxing
|
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(..., new object { ..., inoutCancel});
bool cancel = (bool)inoutCancel;
"inoutCancel" is always returned as "false". Any feedback would be
|
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 non-generic (non C++ template)
std::vector like capability for each of these custom classes. (Solution must
work in Visual Studio 2002).
Since I have already written one std::vector for a YeOlde C++ compiler (Borland
C++ 1.0) that had neither...
| |
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 biggest problem with this process is that the terms "value type"
and "reference type" mean something entirely different than what they mean on
every other platform in every other language. Normally a value type is the
actual data itself stored in...
|
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 delegates were given CDs containing a
pre-release version of the .NET framework/SDK and Visual Studio.NET.
· When was the first version of .NET released?
The final version of the 1.0 SDK and runtime was made publicly
available around 6pm PST on...
|
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
behind going for a different convention in .NET. The recommendations
do make sense (to me, at least but I may be wrong and would like
someone to correct me).
Scope Prefixes
Member Variable Prefix: Most of the people still use (and like the
idea...
|
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 conditions are met (to cancel operation). This parameter is not
being passed back when AppendLog2 invokes itself, which is everytime
due to the operation.
Any ideas how I could better accomplish this? I followed this article:...
|
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: 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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |