Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Decrement operator
Message
From
02/09/2008 08:21:04
 
 
To
02/09/2008 08:01:16
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Vista
Network:
Windows 2008 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01344042
Message ID:
01344059
Views:
18
>>>>Hi,
>>>>Given:
>>>>private int scaleFactor = 1;
>>>>public int ScaleFactor
>>>>        {
>>>>            get { return scaleFactor; }
>>>>            set
>>>>            {
>>>>                if (value == 0)
>>>>                {
>>>>                    scaleFactor = scaleFactor == 1 ? -1 : 1;
>>>>                }
>>>>                else
>>>>                    scaleFactor = value;
>>>>            }
>>>>        }
Why isn't i = -1 after
int i = --ScaleFactor;
>>>
>>>Where is i ?
>>
>>Sorry. Not sure what you mean. If you mean where is the 'int i = --ScaleFactor' line then it's in a button click but I don't see the relevance
>>Regards,
>>Viv
>
>
>I misunderstood i = -1 to be an assignment rather than i == -1 a comparaison
>
>It's a weird construct with side effects - it's skipping zero
>
>You decrement the ScaleFactor, a zero is in i, the zero gets assigned back to ScaleFactor, but the Set has a side effect and assigns it -1 instead of zero

That was the whole point of the accessor.
>
>The decrement operator does not 're-fetch' the value of ScaleFactor after assignment

Which seems to contradict the documentation.

>If other values (2, 3, ...) are allowed, I would suggest this code, since you never know, without testing, when it was zero
>
>--ScaleFactor;
>int i = ScaleFactor;
>
That's what I'm using at the moment

>Update: If you put a breakpoint on
int i = --ScaleFactor;
and step through it, you'll see how it works

Did that before I posted. Istill think the behaviour is at odds with the docs. From the MS C# Language reference:
<i> -- var </i>
Where 
<i>var</i>
An expression that denotes a storage location <b>or a property</b> or an indexer

The first form is a prefix decrement operation. The result of the operation is the value of the operand <i>after</i> it has been decremented.
Best,
Viv
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform