473,396 Members | 2,011 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,396 software developers and data experts.

Cannot modify the result of an unboxing conversion?

error CS0445: Cannot modify the result of an unboxing conversion

I'm quite new to C# and can't really see how to get rid of the above error.

I have an ArrayList instance to which I add two types of elements. The first
type of element is a class, the other type of element is a struct. Sometimes
I need to change the field values of the elements held by the ArrayList. In
the case of the class type element, this is no problem. However, in the case
of the struct type element, C# won't allow it. First of all, I don't really
understand why. Secondly, do I really have to create a new class with
similar content of the struct to get rid of this error?

Regards Carl Johansson
Jun 20 '07 #1
6 12343

Håkan Johansson wrote:
error CS0445: Cannot modify the result of an unboxing conversion

I'm quite new to C# and can't really see how to get rid of the above error.

I have an ArrayList instance to which I add two types of elements. The first
type of element is a class, the other type of element is a struct. Sometimes
I need to change the field values of the elements held by the ArrayList. In
the case of the class type element, this is no problem. However, in the case
of the struct type element, C# won't allow it. First of all, I don't really
understand why. Secondly, do I really have to create a new class with
similar content of the struct to get rid of this error?

Regards Carl Johansson
Hi,
I guess you wrote something like this:
((SomeStruct)ar[0]).Field1 = ...;

Change it to:
SomeStruct s1 = (SomeStruct)ar[0];
s1.Field1 = ...;

Regards, Mykola
http://marss.co.ua

Jun 20 '07 #2
On Jun 20, 10:06 am, "Håkan Johansson"
<carl.johans...@nogarbagehallde.comwrote:
error CS0445: Cannot modify the result of an unboxing conversion

I'm quite new to C# and can't really see how to get rid of the above error.

I have an ArrayList instance to which I add two types of elements. The first
type of element is a class, the other type of element is a struct. Sometimes
I need to change the field values of the elements held by the ArrayList. In
the case of the class type element, this is no problem. However, in the case
of the struct type element, C# won't allow it. First of all, I don't really
understand why. Secondly, do I really have to create a new class with
similar content of the struct to get rid of this error?
Firstly, having a mutable struct is generally a bad idea - it's likely
to bite you sooner or later. This is an example of that.

When you fetch the element from the ArrayList and unbox it, the value
you've got back is now completely independent of the value in the
ArrayList - it's just a copy. If you change the value of a field
within that copy, it won't do what you want. So, you tend to need to
do things like this:

MyStruct x = (MyStruct) arrayList[5];
x.SomeProperty = 20;
arrayList[5] = x;

Now this problem doesn't apply to an array because array elements
effectively count as variables.

All of this is avoided by banning mutable value types. You still need
to change the value in the ArrayList manually, but you don't get into
diffculties working out the different situations and whether
something's made a copy or not.

Jon

Jun 20 '07 #3
Hi,

I would strongly advice agains using a collection with two different types,
even more if one is a reference and the other is a value type.

Can you consolidate in one one type?

Otherwise consider using a wrapper class.

"Håkan Johansson" <ca************@nogarbagehallde.comwrote in message
news:eV**************@TK2MSFTNGP05.phx.gbl...
error CS0445: Cannot modify the result of an unboxing conversion

I'm quite new to C# and can't really see how to get rid of the above
error.

I have an ArrayList instance to which I add two types of elements. The
first
type of element is a class, the other type of element is a struct.
Sometimes
I need to change the field values of the elements held by the ArrayList.
In
the case of the class type element, this is no problem. However, in the
case
of the struct type element, C# won't allow it. First of all, I don't
really
understand why. Secondly, do I really have to create a new class with
similar content of the struct to get rid of this error?

Regards Carl Johansson

Jun 20 '07 #4
Dear Jon!

Thank you for your reply! It was very useful! A resulting question; what is
meant by "mutable" value types? From the context of your reply I suppose it
means that value type variables such as int and struct are inherently
mutable, i.e. can be assigned to an object variable? (Also known as boxing,
no?) Am I right, or are there also "non-mutable" value types?

Regards Carl Johansson
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:11*********************@q75g2000hsh.googlegro ups.com...
On Jun 20, 10:06 am, "Håkan Johansson"
<carl.johans...@nogarbagehallde.comwrote:
error CS0445: Cannot modify the result of an unboxing conversion

I'm quite new to C# and can't really see how to get rid of the above
error.

I have an ArrayList instance to which I add two types of elements. The
first
type of element is a class, the other type of element is a struct.
Sometimes
I need to change the field values of the elements held by the ArrayList.
In
the case of the class type element, this is no problem. However, in the
case
of the struct type element, C# won't allow it. First of all, I don't
really
understand why. Secondly, do I really have to create a new class with
similar content of the struct to get rid of this error?
Firstly, having a mutable struct is generally a bad idea - it's likely
to bite you sooner or later. This is an example of that.

When you fetch the element from the ArrayList and unbox it, the value
you've got back is now completely independent of the value in the
ArrayList - it's just a copy. If you change the value of a field
within that copy, it won't do what you want. So, you tend to need to
do things like this:

MyStruct x = (MyStruct) arrayList[5];
x.SomeProperty = 20;
arrayList[5] = x;

Now this problem doesn't apply to an array because array elements
effectively count as variables.

All of this is avoided by banning mutable value types. You still need
to change the value in the ArrayList manually, but you don't get into
diffculties working out the different situations and whether
something's made a copy or not.

Jon
Jun 20 '07 #5
"Carl Johansson" <ca************@nogarbagehallde.comschrieb im Newsbeitrag
news:u1**************@TK2MSFTNGP04.phx.gbl...
Dear Jon!

Thank you for your reply! It was very useful! A resulting question; what
is meant by "mutable" value types? From the context of your reply I
suppose it means that value type variables such as int and struct are
inherently mutable, i.e. can be assigned to an object variable? (Also
known as boxing, no?) Am I right, or are there also "non-mutable" value
types?

Regards Carl Johansson
A non-mutable type means a type, where the only way to change the content is
to assign a new value.

In a mutable value-type you could assign to a single field of that type
(either direct or through a property). But that still would be a new value.
In your example you would assign to a field of an unboxed value, but that
wouldn't change the value in the list. If the value type were immutable, you
couldn't assign to a simple field in the first place, and therefor had to
assign a whole value, (wich now still is possible.)

Christof
Jun 20 '07 #6
Mutable means changeable. As an example, a Form is mutable: you can
change the caption (Text) etc after it has been created. Many (the
majority of) classes are mutable.

Immutable is the opposite; you cannot change the value, although you
might be able to re-construct another similar. DateTime is an example:
you can't *change* the "seconds" (for example) of an existing
DateTime - you can, however, swap the structure for a different one,
created either via a
constructor, or by calling one of the Add... methods. Many (the
majority of, but sadly [IMO] not all) structs are immutable.

Marc
Jun 20 '07 #7

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

Similar topics

3
by: Greg Scharlemann | last post by:
Does the redirect statement: header(Location:"http://www.newpage.com"); need to come before certain statements? I've setup a login page and try to redirect a user once they have logged in...
1
by: Alexandru Nedelcu | last post by:
Hi!!! I'm trying to add a UserControl created using the Page.LoadCotnrol("test.ascx") method to a Page's Control collection and the exception I get is ( the complete stack trace): ...
1
by: VitorJOL | last post by:
Hello I have a listbox with a datasource.I want to remove a row from listbox but not remove it from the database. How can i do this ? PROBLEM:"Cannot modify the Items collection when the...
5
by: Chris Robb | last post by:
I'm having some really odd behavior with a PHP script. I have it populating an HTML select form with a for loop. When I try the years 2006 to 1900. I get the following error: PHP Warning: ...
5
by: J Huntley Palmer | last post by:
I am getting this error: Cannot modify header information - headers already sent by when I issue a header ("Location: http://www.foobar.com"); How can I redirect the user to such a site...
2
by: mankolele | last post by:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Simple Database Connection</title> </head> <body bgcolor="white"> <?php $connection =...
3
by: TechnoAtif | last post by:
Hi All. I have got problem with the header. If i run my php script in the local host it runs perfectly but if i do the same on net after loading it via ftp. it gives following error: The code...
22
by: dfm | last post by:
Hi, I cant change the header to display images from a database. It's giving me this error : Warning: Cannot modify header information - headers already sent by (output started at...
10
by: jessica87 | last post by:
hi there, i m using this coding to retrieve the file from database so that my user can download the file... <?php if (!isset($_GET)) die('Invalid Parameter'); include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
Oralloy
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,...
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...
0
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...
0
agi2029
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,...
0
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...

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.