Creating Class C#-Sharp

in creating •  7 years ago  (edited)

Make sure to delete the comments tag " //,/./ "

/*
Part One: Creating A Class:

  1. THIS IS HOW YOU CREATE A CLASS
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. //business Rules
  8. namespace DataApp
  9. {
  10. class Date
    
  11. {
    
  12.     // list data members
    
  13.     private int month;
    
  14.     private int day;
    
  15.     private int year;
    
  16.     private string monthName;
    
  17.     // Default constructor
    
  18.     public Date()
    
  19.     {
    
  20.     }
    
  21.     // Other Constructors
    
  22.     public Date(int m, int d, int y)
    
  23.     {
    
  24.         month = m;
    
  25.         day = d;
    
  26.         year = y;
    
  27.     }
    
  28.     public Date(string mon, int d, int y)
    
  29.     {
    
  30.         monthName = mon;
    
  31.         day = d;
    
  32.         year = y;
    
  33.     }
    
  34.     // followed by Special methods getter and setter (PROPERTY)
    
  35.     public int Month
    
  36.     {
    
  37.         get
    
  38.         {
    
  39.             return month
    
  40.         }
    
  41.         set
    
  42.         {
    
  43.             month = value;
    
  44.         }
    
  45.     }
    
  46.     public int Day
    
  47.     {
    
  48.         get
    
  49.         {
    
  50.             return day
    
  51.         }
    
  52.         set
    
  53.         {
    
  54.             day = value;
    
  55.         }
    
  56.     }
    
  57.     public int Year
    
  58.     {
    
  59.         get
    
  60.         {
    
  61.             return year
    
  62.         }
    
  63.         set
    
  64.         {
    
  65.             year = value;
    
  66.         }
    
  67.     }
    
  68.     public string MonthName
    
  69.     {
    
  70.         get
    
  71.         {
    
  72.             return monthName
    
  73.         }
    
  74.         set
    
  75.         {
    
  76.             monthName = value;
    
  77.         }
    
  78.     }
    
  79.     public string ReturnLongDate() //Displaying result
    
  80.     {
    
  81.         return monthName + " " + day + " " + year;
    
  82.     }
    
  83.     public override string ToString()
    
  84.     {
    
  85.         return month + "/" + day + "/" + year;
    
  86.     }
    
  87. }
    
  88. }

Part Two: Applying the Class

  1. This is How You Apply it to the MAIN function.
  2. using System;
  3. using static System.Console;
  4. namespace DataApp
  5. {
  6.  class DataApp
    
  7.  {
    
  8.      static void Main(string[] args)
    
  9.     {
    
  10.         // Creating an object of the class name version
    
  11.         Date aDate = new Date();
    
  12.         aDate.Month = AskForInput("Month");
    
  13.         aDate.Day = AskForInput("Day");
    
  14.         aDate.Year = AskForInput("Year");
    
  15.         WriteLine("\n\tFirst Test");
    
  16.         WriteLine(aDate);
    
  17.         // Create an object of the class version 2
    
  18.         Date secondDate = new Date();
    
  19.         secondDate.Month = 3;
    
  20.         secondDate.Day = 20;
    
  21.         secondDate.Year = 2017;
    
  22.         WriteLine("\n\tSecond Test");
    
  23.         WriteLine(secondDate);
    
  24.         // create an object of the class data version 3
    
  25.         WriteLine("\n\tThird Test");
    
  26.         Date anotherDate = new Date(2, 2, 2017);
    
  27.         WriteLine("Date: " + anotherDate.ToString());
    
  28.         //Create an object of the class data version 4
    
  29.         WriteLine("\n\t4h Test");
    
  30.         Date lastOne = new Date("October", 6, 2019);
    
  31.         WriteLine(lastOne.ReturnLongDate());
    
  32.         ReadKey();
    

//41.
//42. }
//43.
//44. // user input
//45. static int AskForInput(string WhichOne)
//46. {
//47. string inValue;
//48. WriteLine("Enter a number representing the {0}", WhichOne);
//49. inValue = ReadLine();
//50. return (int.Parse(inValue));
//51.
//52. }
//53. }
//54. }

Thats How You Create A Simple Class With C#

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!