Advertisement
MagnusArias

SBO | CLR C#

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