Object-Oriented Programming and C-Based Languages

Notes, Quotes, Summaries and Code


star





Notes from my Exercise Book

Warning: These notes are in no particular order and may confuse you!




Programming languages nowadays are typically part of an IDE (Integrated Development Environment)
Visual Studio is one such IDE
C# is a C-based language, meaning it relies heavily on Object-Oriented Programming
In it, we define objects and then assign values to them
There is a definite sequential flow of instructions in a computer program
Programming utilises resources, objects, variables, etc.
A "solution" is a grouping of more than one programs (classes, forms, etc.)
MS Visual Studio creates forms by default and lets you create a program with a GUI
When creating a project, be sure to specify a project name and appropriate save location
C# underlines errors in your code as you code
Code is inserted into an "event handler"
The program references other code modules behind the scenes
Properties of forms can be adjusted and labels (with customisable text) can be added
Reference different properties of objects with a dot "." e.g. txtSurname.Text
Properties - MaxLength limits the amount of characters input into a text box
You can even do Properties - Enabled: False to totally disable any element from the toolbox
Use ampersands for shortcut keys (in the Text properties)
AcceptButton and CancelButton set default buttons for the forms
"Tab order" is in the View menu

Most programming languages introduce us to data types, e.g. Booleans, integrals, floating point, decimal; string type; arrays
Constants represent unchangeable values: one uses e.g. FILE_NAME
Constants give us control of how a value is used in multiple instances
Naming conventions for local variables e.g. Int32 iValue1; iValue2; iResult (i is for integer)
A 32-bit integer is any number up to 2^32
An integer has no decimal points, whereas a real number (e.g. a float) has decimal points
A string is any characters; we can go back to any data type with a string
For string-type variables, use double quotation marks
For char-type variables, use single quotation marks
Comparison is done with double equals, assignment with double equals
/0 resets a password character to a default state
Comments with //

Insert a break point to check values of variables
Visual C# error notation is very thorough as well as easy to understand

For error handling, use a try/catch(/finally) structure

Strings can be passed to procedures (methods)
A procedure is the same as a routine, a sub-routine, and a function
MessageBox("Your text here").Show is a pre-defined function that brings up a dialog box
A function is called as necessary and values are passed to it
The code to exit a MessageBox is Close();
The code to exit the application is Application.Exit();

The code to clone frHome is:

frmHome objFrm = new frmHome
objFrm.Show();

A struct is like a "lightweight class"
An if(){ } statement is known as conditional branching and can be drawn diagramatically



Some Object-Oriented C# Code



Let's continue by looking at the result of some Windows Forms Design and the C# programming in the background. This program is a Greek alphabet tester, designed to teach the beginner the Greek alphabet so that he or she can gain some knowledge about advanced scientific and mathematical topics that utilise this alphabet in various ways. Note: The programming language we are using is called C# ("C Sharp"), which is mainly intended for development of Windows applications and software; the program in use in the screenshot is called Microsoft Visual Studio, an "IDE."

Programming languages are typically part of an IDE (Integrated Development Environment), which "brings all of the programmer's tools [editor, compiler, linker and debugger] into one place." (http://cplus.about.com)


VStudio snip of Sigma alphabet info

Figure 1: Screenshot of a Visual Studio window containing information about the Greek letter Sigma.


How do we do something like this, whereby if a user selects an item from a combo-box (box with drop-down items, i.e. letters in this case), the whole form changes such that the text boxes below the combo-box show what the Greek letter looks like; the top right text box contains information about its pronunciation, and the bottom right text box contains information about each letter? Bearing in mind that we want this data to be displayed for every letter that can be selected, and that there are twenty-four letters in the Greek alphabet. Without further ado, here's the code:

private void cboGkAlphabet_SelectedIndexChanged(object sender, EventArgs e)
{

try
{
// If the first item of the ComboBox is selected, then process it differently to all the other
// elements, as the first item only contains pure text: "[Please select a letter]"


if (cboGkAlphabet.SelectedIndex == 0)
{
txtGkUpper.Text = "";
txtGkLower.Text = "";
txtPron.Text = "";
txtInfo.Text = "";
}
else
{
// Store the selected object from the combobox to local
// variable named oAlphabet of class type CAlphabet...


// CAlphabet oAlphabet = (CAlphabet)cboGkAlphabet.SelectedItem;
// Note that this code is commented out. Let's see what Ramin has to say:


// Leo: Let's break down the above one-liner into several steps so it becomes more clear:

// Retrieve the object attached to whichever item of the combobox that was clicked (selected) by the user,
// and store it to local variable oItem, which is declared as a generic Object type. Object type is the most
// basic type in C# and can store objects of any defined class.

Object oItem = cboGkAlphabet.SelectedItem;

// Now, we know that the only objects that we attached to all (but the first) items of the combobox were of
// CAlphabet class type, so we can effect a conversion (a 'Cast' to be more precise) which allows us to treat
// the same object using its defined class. This allows us to access the properties and methods of its specific
// class. These properties and methods would otherwise not be accessible...

CAlphabet oAlphabet = (CAlphabet)oItem;

txtGkUpper.Text = oAlphabet._gLtrUpper;
txtGkLower.Text = oAlphabet._gLtrLower;
txtPron.Text = oAlphabet._Pron;
txtInfo.Text = oAlphabet._Info;
}
}

catch
{
MessageBox.Show("Error occurred!");
}
}

// Comments in bold by my lecturer Ramin Majidi (thanks =D)

I have used bold type for the comments because they explain just about everything that's going on here. I suppose you could say that we are populating the fields, or rather the text boxes, with the data stored in the array object, which makes it accessible outside of its locality. Those are the best terms I can explain it with. And until and if I find it necessary to go into detail about classes, objects and arrays, let's look at C and C-based languages.



C# Compared to Other C-Based Languages



Microsoft touts the benefits of its C# programming development environment as follows (see http://msdn.microsoft.com): Additionally, though they all have separate pros and cons, there are several reputed benefits of C# compared to other C-based languages, which Microsoft claims are as follows (see http://msdn.microsoft.com):

See here for the differences between C and Java.

The advanced programmer may find it helpful to know that the following are found in C# but not in Java: nullable value types, enumerations, delegates, lambda expressions and direct memory access. Additionally, C# supports the concepts of encapsulation, inheritance, and polymorphism.

Examples of C-based languages are: Java, C#, C++, PHP, Python and Perl.



Object-Oriented and Event-Based Programming



"Object-oriented programming" (OOP) is a programming paradigm using 'objects' - data structures consisting of data fields and methods together with their interactions - to design applications and computer programs." (Halvorsen H.P., 2014, ch. 1.4)

Definitions:

C# et al. are also event-based programming languages. To explain:

(Wikipedia) "In an event-driven program, there is generally a main loop that listens for events [like the click of a button], and then triggers a callback function when one of those events is detected."

Definition:

But let's reiterate our definition of an object-oriented programming language. Quote: "[W]what defines a 'pure' object-oriented language anyway? Ask five separate people and you'll most likely get five wrong answers. This is because the requirements of a 'pure' object-oriented are fairly subjective. Here's probably the sixth wrong answer:

From Radeck, K (2003). C# and Java: Comparing Programming Languages. [online source] Available at: https://msdn.microsoft.com/en-us/library/ms836794.aspx (Accessed 12 January 2017.)



© 2016-2020 Leo Coroneos
Certificate IV in Information Technology
South Regional TAFE, Albany WA Australia