473,729 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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:

(StudentLocatio n)(student.stuS chedules[dpdnPeriodSelec tion.SelectedTe xt]).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
7 2077
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:

((StudentLocati on)
student.stuSche dules[dpdnPeriodSelec tion.SelectedTe xt]).location =
txtRoom.Text;

Hope this helps.

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

"Marc W." <ma**********@h otmail.com> wrote in message
news:ua******** ******@TK2MSFTN GP12.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:

(StudentLocatio n)(student.stuS chedules[dpdnPeriodSelec tion.SelectedTe xt]).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
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: (StudentLocatio n)(student.stuS chedules[dpdnPeriodSelec tion.SelectedTe xt]).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:

((StudentLocati on)student.stuS chedules[dpdnPeriodSelec tion.SelectedTe xt]
).location = txtRoom.Text;

HTH,
Tim
--
To email me, make the snot hot.
Nov 15 '05 #3
( (StudentLocatio n)student.stuSc hedules[x]).location = "";
//note the paranthesis.
or
StudentLocation sl;
sl = (StudentLocatio n)student.stuSc hedules[x];
sl.location = "";

--
wjs
....

(StudentLocatio n)(student.stuS chedules[dpdnPeriodSelec tion.SelectedTe xt]).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
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_*******@snot mail.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:

((StudentLocati on)student.stuS chedules[dpdnPeriodSelec tion.SelectedTe xt]
).location = txtRoom.Text;

HTH,
Tim
--
To email me, make the snot hot.

Nov 15 '05 #5
Marc W. <ma**********@h otmail.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
That shows up because structs are copied around. When you write:

((StudentLocati on)(student.stu Schedules[dpdnPeriodSelec tion.SelectedTe xt]))

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**********@h otmail.com> wrote in message
news:Op******** ******@TK2MSFTN GP12.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_*******@snot mail.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:

((StudentLocati on)student.stuS chedules[dpdnPeriodSelec tion.SelectedTe xt]
).location = txtRoom.Text;

HTH,
Tim
--
To email me, make the snot hot.


Nov 15 '05 #7
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8

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

Similar topics

4
3478
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 good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
8
3126
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 field and an event that is fired if the boolean field value changes. The derived SortedList class has an override for the Add method. The Add method adds an event handler for the object's event for each new item added to the list. In the...
4
16313
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;
7
3677
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; but it complains for the following lines
3
1563
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 produced by Regex. The Collection's entries are all valid strings. If I CopyTo to a string array it compiles ok, but fails at run time with an InvalidCastException. It works if I CopyTo an object array. ... does this mean that the MatchCollection's...
4
2197
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:
8
4554
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 explain.) The simple version of the system is like this : A customer inserts HTML code on his webpage, which contacts my statistics server each time the customers website recieves a hit - like a classic "register website traffic" system. ...
22
13116
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 Queue<SubB>.
33
3656
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 serializing the object array). I can think of three ways to do this:
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.