PC Review


Reply
Thread Tools Rate Thread

C# syntax question...

 
 
craig
Guest
Posts: n/a
 
      6th Jan 2005
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!!





 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      6th Jan 2005
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


 
Reply With Quote
 
James Curran
Guest
Posts: n/a
 
      6th Jan 2005
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 Removed)> wrote in message
news:(E-Mail Removed)...
> 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!!
>
>
>
>
>



 
Reply With Quote
 
craig
Guest
Posts: n/a
 
      6th Jan 2005
Thanks! I forgot about boxing.

"James Curran" <(E-Mail Removed)> wrote in message
news:%23XrQe$(E-Mail Removed)...
> 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 Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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!!
>>
>>
>>
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Question oldblindpew Microsoft Access Forms 2 12th Mar 2009 07:50 PM
C# syntax question Sid Price Microsoft VC .NET 2 8th Apr 2007 07:47 PM
Syntax question Pat Walters Microsoft Access Reports 4 24th Nov 2004 06:35 PM
Syntax Question - Novice Question sean Microsoft ASP .NET 1 20th Oct 2003 01:18 PM
Syntax question... Dale Lundgren Microsoft Access Forms 1 25th Sep 2003 02:55 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:31 PM.