Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Decrement operator
Message
From
03/09/2008 05:30:43
 
 
To
02/09/2008 10:28:37
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:
01344410
Views:
10
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.....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform