Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Decrement operator
Message
From
04/09/2008 02:46:19
 
 
To
04/09/2008 01:22:44
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:
01344735
Views:
16
>>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
>
>hi Viv,
>
>You can get rid of the odd syntax by implementing an implicit (cast) from NonZero to int
>
>Here's what I have
>
>namespace GregoryAdam.Base.Types
>{
>	public class NonZero
>	{
>		private int nz = 1;
>
>		#region Constructor
>		//-------------------------------------------------------------------------
>		public NonZero(int i)
>		{
>			nz = (i == 0) ? 1 : i;
>		}
>		//-------------------------------------------------------------------------
>		public NonZero()
>		{
>		}
>		//-------------------------------------------------------------------------
>		#endregion
>		//-------------------------------------------------------------------------
>
>		//-------------------------------------------------------------------------
>		public override string ToString()
>		{
>			return nz.ToString();
>		}
>		//-------------------------------------------------------------------------
>
>		#region Cast implementation
>		//-------------------------------------------------------------------------
>		#region (int) NonZero
>		// implicit conversion of NonZero to int
>		public static implicit operator int(NonZero f)
>		{
>			return f.nz;
>		}
>		#endregion
>		//-------------------------------------------------------------------------
>		#region (NonZero) int
>		// explicit conversion of int to NonZero: 0 gets converted into 1
>		// may want to make this implicit
>		public static explicit operator NonZero(int i)
>		{
>			return new NonZero(i);
>		}
>		#endregion
>		//-------------------------------------------------------------------------
>		#endregion	Cast
>
>		#region Operators
>		#region ++
>		//-------------------------------------------------------------------------
>		public static NonZero operator ++(NonZero f1)
>		{
>#if false // this were a structure
>			// -1 + 1 = 0 >> 1 handled in constructor
>			return new NonZero(f1.nz + 1);
>#else
>			if (++f1.nz == 0)
>				f1.nz = 1;
>
>			return f1;
>#endif
>		}
>		//-------------------------------------------------------------------------
>		#endregion
>		#region --
>		//-------------------------------------------------------------------------
>		public static NonZero operator --(NonZero f1)
>		{
>#if false // this were a structure
>			// handle 1 - 1 = 0 >> -1
>			if (f1.nz == 1)
>				return new NonZero(-1);
>
>			return new NonZero(f1.nz - 1);
>#else
>			if (--f1.nz == 0)
>				f1.nz = -1;
>
>			return f1;
>#endif
>
>		}
>		//-------------------------------------------------------------------------
>		#endregion
>		#endregion Operators
>
>	}
>}
>
>
>test program
>
>class Test
>{
>	
>
>	static void Main(string[] args)
>	{
>		var xx = new NonZero();
>		Console.WriteLine(" xx = {0}", xx);
>
>		var yy = --xx;
>		Console.WriteLine(" xx = {0}", xx);
>		Console.WriteLine(" yy = {0}", yy);
>
>		var zz = yy++;
>		Console.WriteLine(" yy = {0}", yy);
>		Console.WriteLine(" zz = {0}", zz);
>
>		var tt = new NonZero();
>
>		int i = --tt; // implicit conversion
>
>		Console.WriteLine(" tt = {0}", tt);
>		Console.WriteLine(" i = {0}", i);
>
>
>		var ww = new NonZero(3);
>		Console.WriteLine(" ww = {0}", ww);
>		Console.WriteLine(" --ww = {0}", --ww);
>
>		Console.ReadLine();
>
>	}
>}
>
>
>
>I would prefer to make it a structure instead of a 'class'.
>The problem with structures is that you cannot
>- have Field initializers (private int nz = 1;)
>- nor can you have a parameterless constructor (which would set nz = 1)
>
>I had this a lot further, with + and - arithmetic.
>Problem: since zero is skipped
>The difference between (-6) and (4) is 9 since the zero does not count
>
>If you have (-6) and you add 4, we would expect it to be (-2)
>
>If you have 4 and you subtract 6, you end up with (-3) if you skip the zero
>
>So, exchanging the operands yields a different result
>So, I'm stuck here

Hi,
Good idea about using the implicit cast ! The stupid thing is that I spent a chunk of time the day before I was doing this writing implicit casts between fairly complex types - yet it never occurred to me to use it in a simple case like this!

Also, in this particular case the result of subtraction and addition operations is not important - it's only really neccessary to ensure that the zero value is skipped. So the cast is the only bit I needed to have this work as required.
Thank you for the help, Best,
Viv
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform