Differences between revisions 3 and 4
Revision 3 as of 2020-04-17 11:04:38
Size: 1493
Editor: Burathar
Comment:
Revision 4 as of 2020-04-17 11:08:32
Size: 3082
Editor: Burathar
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
== Number Remap Class ==
{{{#!highlight c#
public static class Remap
    {
        private static double Map(double value, double fromSource, double toSource, double fromTarget, double toTarget)
        {
            return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
        }
    }
}}}
Line 12: Line 23:
CustomEventArgs.cs !CustomEventArgs.cs
Line 42: Line 53:
InstanceOwner.cs !InstanceOwner.cs
Line 56: Line 67:
== Number Remap Class ==
== IComparable Class ==
Line 58: Line 70:
public static class Remap
    {
        private static double Map(double value, double fromSource, double toSource, double fromTarget, double toTarget)
        {
            return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
        }
    }
public class Mission : IComparable<Mission>, IComparable
{
  public string Name { get; private set; }
  public DateTime LaunchDate { get; private set; }
  public DateTime ReturnDate { get; private set; }
  public string SpaceShip { get; private set; }

  public List<Astronaut> Astronauts { get; private set; }

  public Mission(string name, DateTime launchDate, DateTime returnDate, string spaceShip)
  {
      Astronauts = new List<Astronaut>();
      Name = name;
      LaunchDate = launchDate;
      ReturnDate = returnDate;
      SpaceShip = spaceShip;
  }

  public int CompareTo(Mission other)
  {
      if (ReferenceEquals(this, other)) return 0;
      if (ReferenceEquals(null, other)) return 1;
      int nameComparison = string.Compare(Name, other.Name, StringComparison.Ordinal);
      if (nameComparison != 0) return nameComparison;
      int launchDateComparison = LaunchDate.CompareTo(other.LaunchDate);
      if (launchDateComparison != 0) return launchDateComparison;
      int returnDateComparison = ReturnDate.CompareTo(other.ReturnDate);
      if (returnDateComparison != 0) return returnDateComparison;
      return string.Compare(SpaceShip, other.SpaceShip, StringComparison.Ordinal);
  }

  public int CompareTo(object obj)
  {
      if (ReferenceEquals(null, obj)) return 1;
      if (ReferenceEquals(this, obj)) return 0;
      if (!(obj is Mission)) throw new ArgumentException($"Object must be of type {nameof(Mission)}");
      return CompareTo((Mission) obj);
  }
}

Description

The snippets on this wikipage are written for the .NET Framework v4.0 and higher, but most probably work in older versions. Most will, however not work for .NET Core and .NET Standard.

Snippets

Number Remap Class

   1 public static class Remap
   2     {
   3         private static double Map(double value, double fromSource, double toSource, double fromTarget, double toTarget)
   4         {
   5             return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
   6         }
   7     }

Custom Events & Handling

CustomEventArgs.cs

   1 public class CustomEventArgs : EventArgs
   2 {
   3     private readonly object item;
   4 
   5     public object Item
   6     {
   7         get { return item; }
   8     }
   9 
  10     public CustomEventArgs(object item)
  11     {
  12         this.item = item;
  13     }
  14 }

Instance.cs

   1 public delegate void NotifyCustomEvent(object sender, CustomEventArgs e);
   2 public event NotifyCustomEvent CustomEvent;
   3 
   4 private void RaiseCustomEvent(obj sender,  obj item)
   5 {
   6     CustomEvent?.Invoke(sender, new CustomEventArgs(item));
   7 }

InstanceOwner.cs

   1 public InsanceOwner()
   2 {
   3   Instance instance = new instance();
   4   instance.CustomEvent += new Instance.NotifyCustomEvent(CustomEventHandler);
   5 }
   6 
   7 public void CustomEventHandler(obj sender, CustomEventArgs e)
   8 {
   9     //Handle that custom shit
  10 }

IComparable Class

   1 public class Mission : IComparable<Mission>, IComparable
   2 {
   3   public string Name { get; private set; }
   4   public DateTime LaunchDate { get; private set; }
   5   public DateTime ReturnDate { get; private set; }
   6   public string SpaceShip { get; private set; }
   7 
   8   public List<Astronaut> Astronauts { get; private set; }
   9 
  10   public Mission(string name, DateTime launchDate, DateTime returnDate, string spaceShip)
  11   {
  12       Astronauts = new List<Astronaut>();
  13       Name = name;
  14       LaunchDate = launchDate;
  15       ReturnDate = returnDate;
  16       SpaceShip = spaceShip;
  17   }
  18 
  19   public int CompareTo(Mission other)
  20   {
  21       if (ReferenceEquals(this, other)) return 0;
  22       if (ReferenceEquals(null, other)) return 1;
  23       int nameComparison = string.Compare(Name, other.Name, StringComparison.Ordinal);
  24       if (nameComparison != 0) return nameComparison;
  25       int launchDateComparison = LaunchDate.CompareTo(other.LaunchDate);
  26       if (launchDateComparison != 0) return launchDateComparison;
  27       int returnDateComparison = ReturnDate.CompareTo(other.ReturnDate);
  28       if (returnDateComparison != 0) return returnDateComparison;
  29       return string.Compare(SpaceShip, other.SpaceShip, StringComparison.Ordinal);
  30   }
  31 
  32   public int CompareTo(object obj)
  33   {
  34       if (ReferenceEquals(null, obj)) return 1;
  35       if (ReferenceEquals(this, obj)) return 0;
  36       if (!(obj is Mission)) throw new ArgumentException($"Object must be of type {nameof(Mission)}");
  37       return CompareTo((Mission) obj);
  38   }
  39 }

Howto/CSharp (last edited 2020-04-20 11:02:18 by Burathar)