Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using ToString in VB.NET code in ASP.NET Web Page
Message
From
18/11/2009 08:51:15
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
ASP.NET
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01435079
Message ID:
01435391
Views:
41
>
>Thanks. Can you post an example from your class with other operators (+,-, etc.) defined to complete the lesson? And please give a link (and if possible in VB.NET as well - since I may work with it too).
>
>Thanks again.


Just wrote it - kept it simple
(1) only + and -
(2) implicit cast from int/float/decimal/double to complex()

I leave for you
(1) *, /
(2) IEquatable, IEquatable(T)
(3) ++ and --

Small prog
	class test2
	{
		

		//______________________________________________________________________
		static void Main()
		{
			Complex a = new Complex(1, 1);
			Console.WriteLine("{0}", a);  // (1,1i)
			a += 2;
			Console.WriteLine("{0}", a); // (3,1i)
			Console.ReadLine();
		}
		//______________________________________________________________________
	}
}
namespace GregoryAdam.Base.MathBits
{
	public partial struct Complex
	{
		//______________________________________________________________________
		// properties
		public double Real { get; private set; }
		public double Imaginary { get; private set; }

		//______________________________________________________________________
		// ctor
		public Complex(double i) : this(i, 0.0)
		{
		}
		//______________________________________________________________________
		public Complex( double i, double j ): this()
		{
			Real = i;
			Imaginary = j;
		}
		//______________________________________________________________________

		//______________________________________________________________________
		public override int GetHashCode()
		{
			return Real.GetHashCode()  ^ Imaginary.GetHashCode();
		}
		//______________________________________________________________________
		public override string ToString()
		{
			if ( Imaginary == 0.0 )
				return Imaginary.ToString();
			else
				return String.Format("({0},{1}i)", Real, Imaginary);
		}
		//______________________________________________________________________
		
		//______________________________________________________________________
		// operator addition
		public static Complex operator + (Complex a, Complex b)
		{
			return new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
		}
		//______________________________________________________________________
		// operator subtraction
		public static Complex operator -( Complex a, Complex b )
		{
			return new Complex(a.Real - b.Real, a.Imaginary - b.Imaginary);
		}
		//______________________________________________________________________

		//______________________________________________________________________
		// catches all implicit casts (int, float, decimal >> double)
		// implicit cast from double to Complex
		public static implicit operator Complex( double d )
		{
			return new Complex(d);
		}
		//______________________________________________________________________
	}
}
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform