Advertisement
MagnusArias

SBO | CLR 2 C#

Jan 11th, 2020
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using Microsoft.SqlServer.Server;
  2. using System;
  3. using System.Data.SqlTypes;
  4. using System.Globalization;
  5. using System.Text;
  6.  
  7.  
  8. [Serializable]
  9. [SqlUserDefinedType(Format.Native,
  10. IsByteOrdered = true, ValidationMethodName = "SprawdzPunkt")]
  11. public struct Punkt : INullable
  12. {
  13.     private bool is_Null;
  14.     private double _x;
  15.     private double _y;
  16.     public bool IsNull
  17.     {
  18.         get
  19.         { return (is_Null); }
  20.     }
  21.     public static Punkt Null
  22.     {
  23.         get
  24.         {
  25.             Punkt pt = new Punkt();
  26.             pt.is_Null = true;
  27.             return pt;
  28.         }
  29.     }
  30.     public override string ToString()
  31.     {
  32.         if (this.IsNull)
  33.             return "NULL";
  34.         else
  35.         {
  36.             StringBuilder builder = new StringBuilder();
  37.             builder.Append(_x);
  38.             builder.Append(",");
  39.             builder.Append(_y);
  40.             return builder.ToString();
  41.         }
  42.     }
  43.     [SqlMethod(OnNullCall = false)]
  44.     public static Punkt Parse(SqlString s)
  45.     {
  46.         if (s.IsNull)
  47.             return Null;
  48.         Punkt pt = new Punkt();
  49.         CultureInfo ci = new CultureInfo("en-US");
  50.         string[] xy = s.Value.Split(",".ToCharArray());
  51.         pt.X = double.Parse(xy[0], ci);
  52.         pt.Y = double.Parse(xy[1], ci);
  53.         if (!pt.SprawdzPunkt())
  54.             throw new ArgumentException("Invalid XY coordinate values.");
  55.         return pt;
  56.     }
  57.     // współrzędne X i Y są ustawiane jako właściwościtypu.
  58.     public double X
  59.     {
  60.         get
  61.         { return this._x; }
  62.         set
  63.         {
  64.             double temp = _x;
  65.             _x = value;
  66.             if (!SprawdzPunkt())
  67.             {
  68.                 _x = temp;
  69.                 throw new ArgumentException("Zła współrzędna X.");
  70.             }
  71.         }
  72.     }
  73.     public double Y
  74.     {
  75.         get
  76.         { return this._y; }
  77.         set
  78.         {
  79.             double temp = _y;
  80.             _y = value;
  81.             if (!SprawdzPunkt())
  82.             {
  83.                 _y = temp;
  84.                 throw new ArgumentException("Zła współrzędna Y.");
  85.             }
  86.         }
  87.     }
  88.     // metoda walidująca współrzędne X Y
  89.     private bool SprawdzPunkt()
  90.     {
  91.         return true;
  92.         /*
  93.         if ((_x >= 0) && (_y >= 0))
  94.         { return true; }
  95.         else
  96.         { return false; }
  97.         */
  98.     }
  99.     // Odległość od 0,0.
  100.     [SqlMethod(OnNullCall = false)]
  101.     public Double Odleglosc()
  102.     { return OdlegloscOdXY(0, 0); }
  103.     // Odległość od wskazanego punktu
  104.     [SqlMethod(OnNullCall = false)]
  105.     public Double OdlegloscOd(Punkt pFrom)
  106.     { return OdlegloscOdXY(pFrom.X, pFrom.Y); }
  107.     // Odległość od wskazanego punktu.
  108.     [SqlMethod(OnNullCall = false)]
  109.     public Double OdlegloscOdXY(double iX, double iY)
  110.     {
  111.         return Math.Sqrt(Math.Pow(iX - _x, 2.0) + Math.Pow(iY - _y, 2.0));
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement