Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Decrement operator
Message
From
03/09/2008 06:11:27
 
 
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:
01344413
Views:
11
>Hi,
>
>>The only way out - as far a I can see apart from splitting it into two instructions - is to define a class or a structure ScaleFactor and define the operators
>
>That's what I was thinking but it gets ugly :-}
>
    public class NonZero
>    {
>        private int i = 1;
>        public int I
>        {
>            get { return i; }
>            set { i = value; }
>        }
>
>        public static NonZero operator ++(NonZero nz)
>        {
>            nz.i = nz.i == -1 ? 1 : nz.i+1;
>            return nz;
>        }
>
>        public static NonZero operator --(NonZero nz)
>        {
>            nz.i = nz.i == 1 ? -1 : nz.i - 1;
>            return nz;
>        }
>    }
>works but requires odd syntax: int i = (NonZeroInstance++).I
>This one also works but isn't exactly intuitive ! :
    public class AutoIncr
>    {
>        int i = 1;
>
>        public static int operator +(AutoIncr a, int ini)
>        {
>            a.i = a.i + ini;
>            a.i = a.i==0 ? 1 : a.i;
>            return a.i;
>        }
>        public static int operator +(int ini, AutoIncr a)
>        {
>            int ret = a.i;
>            a.i = a.i + ini;
>            a.i = a.i == 0 ? 1 : a.i;
>            return ret;
>        }
>        public static int operator -(AutoIncr a, int ini)
>        {
>            a.i = a.i - ini;
>            a.i = a.i == 0 ? -1 : a.i;
>            return a.i;
>        }
>        public static int operator -(int ini, AutoIncr a)
>        {
>            int ret = a.i;
>            a.i = a.i - ini;
>            a.i = a.i == 0 ? -1 : a.i;
>            return ret;
>        }
>    }
>Gives behaviour similar to a prefix and postfix increments since AutoIncrInstance+1 is different to 1+ AutoIncrInstance and handles changes other than just a simple increment.....


Lemme think, Viv, there must be a way to turn a pigeon into a rabbit

But for now a couple of remarks re the second way

(1) The second method for + (and also for -) should call the first method. (they call it good design) I think small methods get inlined anyway

(2) The methods are not correct as I see it. You test for zero after the addition/subtraction. As I see it you have to test whether the sign has changed

(a) if it hasn't, then it's ok (eg 3 + 3 = 6)

(b) if it has become zero, then you have to test for the previous sign (which cannot be zero)
eg:
3 -3 >> -1
-3 + 3 >> +1

(c) otherwise ( from - to +, + to -)

(c_1) form - to +, add one
eg -3 + 4 = +1 should be +2 (since zero does not exist)

(c_2) from + to -, subtract one
3 - 4 = -1, should be -2 (since zero does not exist)
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform