Casting objects in an array (well, a SortedList) doesn't work

  • Thread starter Thread starter Marc W.
  • Start date Start date
M

Marc W.

I have been trying to cast an object I am getting from a SortedList for some
time now, into the object it is supposed to be, but it just won't work. Here
is the code:

(StudentLocation)(student.stuSchedules[dpdnPeriodSelection.SelectedText]).lo
cation = txtRoom.Text;

Every object in the stuSchedules array is a StudentLocation object. However,
SortedLists store all of their values as objects, so I need to convert it
back into the correct type so I can set one of the fields. However I keep
getting the error that 'object' does not respond to 'location'. I don't
understand how the compiler still thinks I am treating this as an object
because the typecast is right there!

This is getting really very frusterating. I have never had a problem like
this in any other language. So if someone could please give me a hint or
something as to what I am doing wrong, it would be greatly appreciated.

- Marc Weil
 
Marc,

I believe that the typecast is not being performed correctly, that the
property is being executed before the typecast. You should do this, to make
sure:

((StudentLocation)
student.stuSchedules[dpdnPeriodSelection.SelectedText]).location =
txtRoom.Text;

Hope this helps.
 
I have been trying to cast an object I am getting from a SortedList for some
time now, into the object it is supposed to be, but it just won't work. Here
is the code:
(StudentLocation)(student.stuSchedules[dpdnPeriodSelection.SelectedText]).lo
cation = txtRoom.Text;
Every object in the stuSchedules array is a StudentLocation object. However,
SortedLists store all of their values as objects, so I need to convert it
back into the correct type so I can set one of the fields. However I keep
getting the error that 'object' does not respond to 'location'. I don't
understand how the compiler still thinks I am treating this as an object
because the typecast is right there!
This is getting really very frusterating. I have never had a problem like
this in any other language. So if someone could please give me a hint or
something as to what I am doing wrong, it would be greatly appreciated.
- Marc Weil

You are missing/mis-placing one parantheses. Try:

((StudentLocation)student.stuSchedules[dpdnPeriodSelection.SelectedText]
).location = txtRoom.Text;

HTH,
Tim
 
( (StudentLocation)student.stuSchedules[x]).location = "";
//note the paranthesis.
or
StudentLocation sl;
sl = (StudentLocation)student.stuSchedules[x];
sl.location = "";
 
That's actually exactly what I did. And then it told me that the left side
of an assignment had to be a property, indexer, or something else in order
to work. I changed the struct those members were a part of to a class and it
worked... very strange.

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

Why does the compiler hate structs so much? They are in integral part of C,
and are supposedly better than classes for very small custom types that are
needed.
- Marc
 
Marc W. said:
That's actually exactly what I did. And then it told me that the left side
of an assignment had to be a property, indexer, or something else in order
to work. I changed the struct those members were a part of to a class and it
worked... very strange.
The error was "The left-hand side of an assignment must be a variable,
property or indexer."

Why does the compiler hate structs so much? They are in integral part of C,
and are supposedly better than classes for very small custom types that are
needed.

Not when you put them into an ArrayList they're not. The problem you're
running into is boxing and unboxing. If the compiler hadn't complained,
your code wouldn't have had any effect - because in the process of
unboxing, you'd have created a new copy of the struct, which was
entirely distinct from the one actually in the ArrayList. You'd have
then changed the variable within it, and then the struct would have
been impossible to access afterwards.

See section 14.13.1 of the ECMA C# spec for more information.
 
That shows up because structs are copied around. When you write:

((StudentLocation)(student.stuSchedules[dpdnPeriodSelection.SelectedText]))

That means pull the item out of the collection and put it into a local
temporary. If you try to modify a property on this, the compiler won't let
you, because you would only be modifying the *temporary*, not the actual
value.

When you change to a class, this isn't an issue because the temporary copy
is a reference, and the reference doesn't change.

In general, you should write everything as a class, and only switch to a
struct if necessary.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
Marc W. said:
That's actually exactly what I did. And then it told me that the left side
of an assignment had to be a property, indexer, or something else in order
to work. I changed the struct those members were a part of to a class and it
worked... very strange.

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

Why does the compiler hate structs so much? They are in integral part of C,
and are supposedly better than classes for very small custom types that are
needed.
- Marc


Tim Smelser said:
On Thu, 22 Jan 2004 13:29:15 -0500, Marc W. wrote:


You are missing/mis-placing one parantheses. Try:

((StudentLocation)student.stuSchedules[dpdnPeriodSelection.SelectedText]
).location = txtRoom.Text;

HTH,
Tim
 
That is very interesting. I'll be sure to keep that in mind, and only use
structs when absolutely necissary. I guess I just assumed that C# was the
same as C and C++ when dealing with structs and classes.

Thanks for your and Eric's help!
- Marc
 
Back
Top