Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using ToString in VB.NET code in ASP.NET Web Page
Message
De
18/11/2009 08:51:15
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
ASP.NET
Database:
Visual FoxPro
Divers
Thread ID:
01435079
Message ID:
01435391
Vues:
40
>
>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform