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 7 2065
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
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.
( (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
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.
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jacob Jensen |
last post by:
This question has probably been asked a million time, but here it comes
again. I want to learn the difference between the three type cast operators:
static_cast, reinterpret_cast, dynamic_cast. A...
|
by: Dennis C. Drumm |
last post by:
I have a class derived from a SortedList called SystemList that contains a
list of objects indexed with a string value.
The definition of the objects contained in the SortedList have a boolean...
|
by: Fabrizio |
last post by:
Hi
I cannot figure why it isn't possible to cast a struct array to an object
array.
I written a structure like this:
public struct Test {
private int TestA;
private int TestB;
|
by: yufufi |
last post by:
lets say we have a 'shape' class which doesn't implement IComparable
interface..
compiler doesn't give you error for the lines below..
shape b= new shape();
IComparable h;
h=(IComparable)b;...
|
by: Barry Mossman |
last post by:
Hi,
I get the feeling that I am missing something with regards to casting.
The CopyTo method allows me to copy the contents of a collection into an
array. My collection is a MatchCollection...
|
by: Arjen |
last post by:
Hi,
I need to add this inside an array:
1 3
2 4
3 2
4 5
5 1
I think of using this:
|
by: Tony Fonager |
last post by:
I am currently developing a statistics system in ASP.NET, and need to share
information about the customers websites, in this application.
(I have simplified my code, to make my project easier to...
|
by: Adam Clauss |
last post by:
OK, I have class A defined as follows:
class A
{
A(Queue<B> queue) { ... }
}
Now, I then have a subclass of both classes A and B. The subclass of A
(SubA), more specifically is passed a...
|
by: Mark P |
last post by:
A colleague asked me something along the lines of the following today.
For some type X he has:
X* px = new X;
Then he wants to convert px to a char* (I'm guessing for the purpose of...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
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 ...
| | |