C# class library for easier console screen manipulation

(c) 2015 by Barton Paul Levenson


Language:
C#

Dialect:
Microsoft Visual C# Express 2010

Discussion:
This illustrates how to create a dynamic link library (.dll file) in C#.Net. Writing the library code is the easy part. Start the project as a class library instead of a Windows routine or console routine. Leave out any main routine. It will compile into a .dll file instead of an .exe file. Make sure to make the methods you want to call public!

But then things get harder. Not only do you need to include a Using statement above your calling file's namespace declaration. You also have to add a reference to the dll, browsing for where the actual .dll file is located. And then, since your class is a new type you've created, you have to instantiate an object of that type to use it. And you have to do that in the method which calls the routines, not out in the class or the namespace. Putting it there, even declared as "public," will give you an error message for every call to a routine. I illustrate proper syntax for a program to call routines in this thing after the class library itself.

Note that the put routine is overloaded three times, to write strings, integers, or real numbers.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kon
{
    // Console I/O routines, because the .NET ones are verbose.
    public class kon
    {
        // pause lets the user read a screen before going on.
        public void pause()
        {
            Console.SetCursorPosition(0, 23);
            Console.Write("Press [Enter] to go on.  ");
            Console.ReadKey();
        } // pause



        // put (string) prints a string at row r, column c.
        public void put(int r, int c, string st)
        {
            Console.SetCursorPosition(c - 1, r - 1);
            Console.Write(st);
        } // put (string)



        // put (integer) prints an integer at row r, column c.
        public void put(int r, int c, int i, int w)
        {
            Console.SetCursorPosition(c - 1, r - 1);
            Console.Write(i.ToString().PadLeft(w));
        } // put (integer)



        // put (double) prints a real number at row r, column c.
        public void put(int r, int c, double x, int w, int d)
        {
            string fmt = 'f' + d.ToString();

            Console.SetCursorPosition(c - 1, r - 1);
            Console.Write(x.ToString(fmt).PadLeft(w));
        } // put (double)
    } // kon class
} // Kon namespace



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kon;

namespace KonTester
{
    class KonTest
    {
        static void Main(string[] args)
        {
            kon con = new kon();

            con.put(2, 5, "Hi, there!"); // Puts message in row 2, column 5.
            con.pause();
        }
    } // KonTest class
} // KonTester namespace


Page created:09/16/2015
Last modified:  09/16/2015
Author:BPL