By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,798 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,798 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc W." <ma**********@hotmail.com> wrote in message
news:ua**************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #2

P: n/a
On Thu, 22 Jan 2004 13:29:15 -0500, Marc W. wrote:
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
--
To email me, make the snot hot.
Nov 15 '05 #3

P: n/a
( (StudentLocation)student.stuSchedules[x]).location = "";
//note the paranthesis.
or
StudentLocation sl;
sl = (StudentLocation)student.stuSchedules[x];
sl.location = "";

--
wjs
....

(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

Nov 15 '05 #4

P: n/a
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" <t_*******@snotmail.com> wrote in message
news:14******************************@40tude.net.. .
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
--
To email me, make the snot hot.

Nov 15 '05 #5

P: n/a
Marc W. <ma**********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
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." <ma**********@hotmail.com> wrote in message
news:Op**************@TK2MSFTNGP12.phx.gbl...
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" <t_*******@snotmail.com> wrote in message
news:14******************************@40tude.net.. .
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
--
To email me, make the snot hot.


Nov 15 '05 #7

P: n/a
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.