I am using an ArrayList named "nodeRanks" of the following structs:
private struct NodeRank
{
public TreeNode Node;
public Int32 Rank;
}
I sort the nodeRanks ArrayList based upon the rank values, and then I would
like to replace the rank values with the ArrayList index values. However,
when I try the following approach, I get a runtime error:
for(int i=0;i<=nodeRank.Count-1;i++)
{
((NodeRank)nodeRanks[i]).rank = i;
}
The error that I get is: (3696): The left-hand side of an assignment must
be a variable, property or indexer
I am not sure why I am getting this error because the left side of my
assignment is a public field (variable) of the struct. If I re-write the
code as follows, it works. But I would still like to know why the more
consise syntax fails.
NodeRank tempNodeRank;
for(int i=0;i<=nodeRank.Count-1;i++)
{
tempNodeRank = (NodeRank)nodeRanks[i];
tempNodeRank.rank = i;
nodeRanks[i] = tempNodeRank;
}
Thanks!! 2 1166
craig wrote: I am using an ArrayList named "nodeRanks" of the following structs:
private struct NodeRank { public TreeNode Node; public Int32 Rank; }
I sort the nodeRanks ArrayList based upon the rank values, and then I would like to replace the rank values with the ArrayList index values. However, when I try the following approach, I get a runtime error: for(int i=0;i<=nodeRank.Count-1;i++) { ((NodeRank)nodeRanks[i]).rank = i; }
The error that I get is: (3696): The left-hand side of an assignment must be a variable, property or indexer
I am not sure why I am getting this error because the left side of my assignment is a public field (variable) of the struct. If I re-write the code as follows, it works. But I would still like to know why the more consise syntax fails.
NodeRank tempNodeRank; for(int i=0;i<=nodeRank.Count-1;i++) { tempNodeRank = (NodeRank)nodeRanks[i]; tempNodeRank.rank = i; nodeRanks[i] = tempNodeRank; }
Thanks!!
I think it works when you change NodeRank to a "class".
ArrayList stores *objects*, which means that structs (as value-types)
are boxed. This is a copy(-like) operation, as there are no references
to value-types. So you original code would update a copy of the
"real" value, and then discard that copy. The version in the ArrayList
is not updated.
Hans Kesting
Thanks! I forgot about boxing.
"James Curran" <Ja*********@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... NodeRank is a struct, so it's a value type. ArrayList holds objects, which are reference types.
To store a value type in a object, it must be boxed.
Writing "tempNodeRank = (NodeRank)nodeRanks[i];" unboxes the struct. "((NodeRank)nodeRanks[i]).rank" is attempting to refernce the still boxed object.
Change NodeRank to a class, and that line will work.
-- Truth, James Curran [erstwhile VC++ MVP] Home: www.noveltheory.com Work: www.njtheater.com Blog: www.honestillusion.com Day Job: www.partsearch.com
"craig" <e@mail.com> wrote in message news:e4**************@TK2MSFTNGP10.phx.gbl... I am using an ArrayList named "nodeRanks" of the following structs:
private struct NodeRank { public TreeNode Node; public Int32 Rank; }
I sort the nodeRanks ArrayList based upon the rank values, and then I would like to replace the rank values with the ArrayList index values. However, when I try the following approach, I get a runtime error:
for(int i=0;i<=nodeRank.Count-1;i++) { ((NodeRank)nodeRanks[i]).rank = i; }
The error that I get is: (3696): The left-hand side of an assignment must be a variable, property or indexer
I am not sure why I am getting this error because the left side of my assignment is a public field (variable) of the struct. If I re-write the code as follows, it works. But I would still like to know why the more consise syntax fails.
NodeRank tempNodeRank; for(int i=0;i<=nodeRank.Count-1;i++) { tempNodeRank = (NodeRank)nodeRanks[i]; tempNodeRank.rank = i; nodeRanks[i] = tempNodeRank; }
Thanks!!
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro...
|
by: Aaron Walker |
last post by:
Greetings,
I'm attempting to write my first *real* template function that also deals with
a map of strings to member function pointers that is making the syntax a little
tricky to get right.
...
|
by: bor_kev |
last post by:
Hi,
First of all, i want to use the new managed class syntax and STL.NET
under Microsoft Visual (C++) Studio 2005 Beta.
I read in a Microsoft...
|
by: r.nikhilk |
last post by:
Hi,
Currently, we are porting C++ applications from 32 bit to 64 bit on AIX
platform. (The current version of AIX is 5.3 and xlC verison is 8.0).
We are able to compile the applications by...
|
by: astromog |
last post by:
I have some significantly extended syntax for Python that I need to
create a reference implementation for. My new syntax includes new
keywords, statements and objects that are sort of like classes...
|
by: deppy_3 |
last post by:
Hi!
The syntax of fputs() is similar with the syntax of fgets();
For example
if we have:fgets(str,maxlen,stdin)
which is the syntax of the fputs();
|
by: Smithers |
last post by:
Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something more
profound than a simple syntax preference of the...
|
by: berrylthird |
last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example:
var myInstance:myClass = new...
|
by: trose178 |
last post by:
Good day all, I am working on a multi-select list box for a standard
question checklist database and I am running into a syntax error in
the code that I cannot seem to correct. I will also note...
|
by: Daniel |
last post by:
I hope this question is OK for this list. I've downloaded Rpyc and
placed it in my site packages dir. On some machines it works fine, on
others not so much.
Here is one error I get when I try...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
| |