Cannot modify the result of an unboxing conversion?

H

Håkan Johansson

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
 
M

marss

Håkan Johansson said:
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
 
J

Jon Skeet [C# MVP]

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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

Carl Johansson

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]" <[email protected]> skrev i meddelandet
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
 
C

Christof Nordiek

Carl Johansson said:
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
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top