Products
Services
Contact Us

Scripts: C# :: Classes :: Library Article #1

Developer's Section

Create a class in C#
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (2019)
Favorites:

This is a short explanation on how to use classes for C#.

This is a simple example on using classes in C#. The first class (myTasks) takes a multidimensional array of tasks as parameter. When calling the printTodaysTasks the class will print the tasks for today. When calling printTomorrowsTasks the class will print the tasks for tomorrow. The initialization class (TestClass) tests myTasks.

Mytasks class C# code:

 Code Snippet 1


// declare namespace
using System;

// **************************
//       Task Class
//
//   by Erobo Team Member
//
// initializes a set of tasks
// and reminds you of any given
// tasks for the day or the next
// day.
//
// **************************


class myTasks
{
    string[,] myTasks;

    // Constructor declaration
    public myTasks(string[,] givenTasks)
    {
        myTasks = givenTasks;
    }

    //master print method
    public void doPrint(day, task)
    {
        string thisTask = "Task for: ";
        thisTask += day
        thisTask += " is: ";
        thisTask += task;
        Console.WriteLine(thisTask);

    }

    // find and print today's tasks
    public void printTodaysTasks()
    {
        // loop through array of tasks
        // and find todays tasks
        
        for (int i = 0; i < scores.Length; i++)
        {
            if( myTasks[i][0] == "today" )
            {
                doPrint(myTasks[i][0],myTasks[i][1]);
            }
        }
    }

    // find and print tomorrow's tasks
    public void printTomorrowsTask()
    {
        // loop through array of tasks
        // and find tomorrows tasks
         for (int i = 0; i < scores.Length; i++)
        {
            if( myTasks[i][0] == "tomorrow" )
            {
                doPrint(myTasks[i][0],myTasks[i][1]);
            }
        }    
    }

    // Destructor declaration
    ~myTasks()
    {
         myTasks = new string[1,2] { {"",""}};
    }
}



TestClass class C# code:

 Code Snippet 2

// Initialization Class
class TestClass
{
    // Main begins program execution.
    public static void Main()
    {

        string[,] weeklyTasks = new string[5, 2] {{"today", "9:30 meeting"},
                                                  {"today", "2:30 pm lunch"}, 
                                                  {"today", "2:45 pm conference"}, 
                                                  {"tomorrow","10:20 am speak M" }, 
                                                  {"tomorrow", "9:00 pm Cocktail"}};

        // Create a new instance of myTasks
        // Use this week tasks to initialize Object
        myTasks thisTasks = new myTasks(weeklyTasks);

        // remind me and print today's tasks
        thisTasks.printTodaysTasks();

        // remind me and print tomorrow's tasks
        thisTasks.printTomorrowsTask();
       
    }
}



See other Scripts in Classes

Submit Your Scripts:


System Message:
  • Your C# script has been sent. Please allow 48 hours for processing.


If you would like to have your Javascripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]