Advertisement
f0rkB0mb

Freddy Gammelfish's C#-Code:

Oct 27th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1. public static class Extensions
  2. {
  3.     public static string GetPropertyName<T>(this Expression<Func<T>> target)
  4.     {
  5.     var memberExpression = target.Body as MemberExpression;
  6.  
  7.         if (memberExpression == null)
  8.         {
  9.             UnaryExpression ubody = (UnaryExpression)target.Body;
  10.             memberExpression = ubody.Operand as MemberExpression;
  11.         }
  12.  
  13.         return memberExpression.Member.Name;
  14.     }
  15.  
  16.     public static int ToInteger(this string target)
  17.     {
  18.         int val;
  19.         if (int.TryParse(target, out val))
  20.             return val;
  21.         else
  22.             return 0;
  23.     }
  24. }
  25.  
  26. public class IntToStringConverter : IValueConverter
  27. {
  28.     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  29.     {
  30.         return value.ToString();
  31.     }
  32.  
  33.     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  34.     {
  35.         return (value as string).ToInteger();
  36.     }
  37. }
  38.  
  39. public class MainViewModel : INotifyPropertyChanged
  40. {
  41.     public MainViewModel()
  42.     {
  43.         this.CalculateCommand = new RelayCommand(this.CalculateAction);
  44.     }
  45.  
  46.     public ICommand CalculateCommand { get; private set; }
  47.  
  48.     private void CalculateAction()
  49.     {
  50.         int mid1 = this.UpperLimit - this.LowerLimit;
  51.         int mid2 = (this.UpperLimit + this.LowerLimit) * 2;
  52.  
  53.         this.MidPoint = (mid1 * this.Function) * mid2;
  54.  
  55.         int trap1 = (this.UpperLimit - this.LowerLimit) * 2;
  56.         int trap2 = (this.Function * this.UpperLimit) + (this.Function * this.LowerLimit);
  57.  
  58.         this.Trapezium = trap1 + trap2;
  59.  
  60.         this.Result = ((2 * this.MidPoint) + this.Trapezium) * 3;
  61.  
  62.         int stepedInt = (this.UpperLimit - this.LowerLimit) * this.Steps;
  63.     }
  64.  
  65.     #region Fucking fields
  66.  
  67.     private int _function;
  68.     private int _lowerLimit;
  69.     private int _midPoint;
  70.     private int _result;
  71.     private int _steps;
  72.     private int _trapezium;
  73.     private int _upperLimit;
  74.  
  75.     #endregion Fucking fields
  76.  
  77.     #region Properties
  78.  
  79.     public int Function
  80.     {
  81.         get { return this._function; }
  82.         set
  83.         {
  84.             if (this._function == value)
  85.                 return;
  86.  
  87.             this._function = value;
  88.             this.RaiseNotifyPropertyChanged(() => this.Function);
  89.         }
  90.     }
  91.  
  92.     public int LowerLimit
  93.     {
  94.         get { return this._lowerLimit; }
  95.         set
  96.         {
  97.             if (this._lowerLimit == value)
  98.                 return;
  99.  
  100.             this._lowerLimit = value;
  101.             this.RaiseNotifyPropertyChanged(() => this.LowerLimit);
  102.         }
  103.     }
  104.  
  105.     public int MidPoint
  106.     {
  107.         get { return this._midPoint; }
  108.         set
  109.         {
  110.             if (this._midPoint == value)
  111.                 return;
  112.  
  113.             this._midPoint = value;
  114.             this.RaiseNotifyPropertyChanged(() => this.MidPoint);
  115.         }
  116.     }
  117.  
  118.     public int Result
  119.     {
  120.         get { return this._result; }
  121.         set
  122.         {
  123.             if (this._result == value)
  124.                 return;
  125.  
  126.             this._result = value;
  127.             this.RaiseNotifyPropertyChanged(() => this.Result);
  128.         }
  129.     }
  130.  
  131.     public int Steps
  132.     {
  133.         get { return this._steps; }
  134.         set
  135.         {
  136.             if (this._steps == value)
  137.                 return;
  138.  
  139.             this._steps = value;
  140.             this.RaiseNotifyPropertyChanged(() => this.Steps);
  141.         }
  142.     }
  143.  
  144.     public int Trapezium
  145.     {
  146.         get { return this._trapezium; }
  147.         set
  148.         {
  149.             if (this._trapezium == value)
  150.                 return;
  151.  
  152.             this._trapezium = value;
  153.             this.RaiseNotifyPropertyChanged(() => this.Trapezium);
  154.         }
  155.     }
  156.  
  157.     public int UpperLimit
  158.     {
  159.         get { return this._upperLimit; }
  160.         set
  161.         {
  162.             if (this._upperLimit == value)
  163.                 return;
  164.  
  165.             this._upperLimit = value;
  166.             this.RaiseNotifyPropertyChanged(() => this.UpperLimit);
  167.         }
  168.     }
  169.  
  170.     #endregion Properties
  171.  
  172.     #region INotifyPropertyChanged Implementation
  173.  
  174.     public event PropertyChangedEventHandler PropertyChanged;
  175.  
  176.     protected void RaiseNotifyPropertyChanged<T>(Expression<Func<T>> propertyExpression)
  177.     {
  178.         if (this.PropertyChanged != null)
  179.         {
  180.             string propertyName = propertyExpression.GetPropertyName();
  181.             this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  182.         }
  183.     }
  184.  
  185.     #endregion INotifyPropertyChanged Implementation
  186. }
  187.  
  188. public class RelayCommand : ICommand
  189. {
  190.     public Action action;
  191.     public Func<bool> canexecute;
  192.  
  193.     public RelayCommand(Action action, Func<bool> canexecute)
  194.     {
  195.         this.action = action;
  196.         this.canexecute = canexecute;
  197.  
  198.         this.OnCanExecuteChanged();
  199.     }
  200.  
  201.     public RelayCommand(Action action)
  202.     {
  203.         this.action = action;
  204.         this.canexecute = () =>
  205.         {
  206.             return true;
  207.         };
  208.  
  209.         this.OnCanExecuteChanged();
  210.     }
  211.  
  212.     public event EventHandler CanExecuteChanged;
  213.  
  214.     public bool CanExecute(object parameter)
  215.     {
  216.         return this.canexecute();
  217.     }
  218.  
  219.     public void Execute(object parameter)
  220.     {
  221.         this.action();
  222.     }
  223.  
  224.     public void RefreshCanExecute()
  225.     {
  226.         this.OnCanExecuteChanged();
  227.     }
  228.  
  229.     protected void OnCanExecuteChanged()
  230.     {
  231.             if (this.CanExecuteChanged != null)
  232.             this.CanExecuteChanged(this, EventArgs.Empty);
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement