Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Decrement operator
Message
From
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:
01344050
Views:
19
>>>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

The decrement operator does not 're-fetch' the value of ScaleFactor after assignment

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;
Update: If you put a breakpoint on
int i = --ScaleFactor;
and step through it, you'll see how it works
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform