C# syntax question...

  • Thread starter Thread starter craig
  • Start date Start date
C

craig

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).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;
tempNodeRank.rank = i;
nodeRanks = tempNodeRank;
}

Thanks!!
 
craig said:
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).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;
tempNodeRank.rank = i;
nodeRanks = 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
 
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;" unboxes the struct.
"((NodeRank)nodeRanks).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
 
Thanks! I forgot about boxing.

James Curran said:
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;" unboxes the struct.
"((NodeRank)nodeRanks).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 said:
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).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;
tempNodeRank.rank = i;
nodeRanks = tempNodeRank;
}

Thanks!!

 
Back
Top