Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Decrement operator
Message
De
03/09/2008 05:30:43
 
 
À
02/09/2008 10:28:37
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Vista
Network:
Windows 2008 Server
Database:
MS SQL Server
Divers
Thread ID:
01344042
Message ID:
01344410
Vues:
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.....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform