Advertisement
kyrathasoft

tester2

Jul 21st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. /* a helper class, Tester, begun in Lesson 5 and now extended in
  3. Lesson 6 of intro C# console programming series found at the
  4. following URL:
  5. http://williammillerservices.com/windows-c-console-programming/
  6.  
  7. GitHub gist -> https://gist.github.com/kyrathasoft/1a2b341e6ae8e97c91c68e51082c1409
  8. Pastebin.com -> https://pastebin.com/nKVkikZ8
  9.  
  10. note: to use, you would reference the file containing this code
  11. from your primary program file. If you have the following program...
  12.  
  13. using System;
  14. using TestInput;
  15.  
  16. namespace MyNS{
  17.     class MyCls{
  18.         public static void Main(){
  19.             string sNum = "4";
  20.             if(Tester.IsInteger(sNum)){
  21.                 Console.WriteLine("Yes, we can parse {0} to an int.", sNum);
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. ...and if above program named test.cs and below is tester.cs and in
  28. same directory, then we'd compile like so:
  29.  
  30. csc /out:test.exe test.cs tester.cs
  31.  
  32. Running resulting test.exe would produce: Yes, we can parse 4 to an int.
  33.  
  34. Or, we could make tester.cs code into DLL via:
  35. csc /t:library tester.cs
  36.  
  37. ...and then compile like so:
  38. csc /r:tester.dll test.cs
  39.  
  40. */
  41.  
  42.  
  43. namespace TestInput
  44. {
  45.     public class Tester
  46.     {
  47.         public static bool IsInteger(string p)
  48.         {
  49.             bool result = false;
  50.             int i;
  51.  
  52.             if (int.TryParse(p, out i))
  53.             {
  54.                 result = true;
  55.             }
  56.  
  57.             return result;
  58.         }
  59.  
  60.     }
  61.    
  62.     public static int GetDifferenceInYears(DateTime startDate, DateTime endDate)
  63. {
  64.         //Excel documentation says "COMPLETE calendar years in between dates"
  65.         int years = endDate.Year - startDate.Year;
  66.  
  67.         if (startDate.Month == endDate.Month &&// if the start month and the end month are the same
  68.             endDate.Day < startDate.Day// AND the end day is less than the start day
  69.             || endDate.Month < startDate.Month)// OR if the end month is less than the start month
  70.         {
  71.             years--;
  72.         }
  73.  
  74.         return years;
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement