J#.Net Tutorial

J#.Net Tutorial #5

Welcome to the fifth J# tutorial (I really must find a better way to start these tutorials). I want to start with something that is very useful particularly if you want to create console based menus (I know that Windows GUI programming does all that but it is a useful construct all the same) which is called case...switch (if you are familiar with Visual Basic or older versions of BASIC then you will know it as case...select). It allows a decision to be made without using a whole lot of if...else statements as follows:-

import System.*;

class hello
{
	public static void main()
	{
		int i=4;

		switch(i)
		{
			case 1:
				Console.WriteLine("The value is 1");
				break;
			case 2:
				Console.WriteLine("The value is 2");
				break;
			case 3:
				Console.WriteLine("The value is 3");
				break;
			default:
				Console.WriteLine("The value isn't 1, 2 or 3");
				break;
		}
	}
}

C# differs from earlier languages in that it insists on the use of the break keyword in each case statement. J# isn't so fussy but still probably good practice to use it. So how does it work? An integer value is set up which is coded to contain the number 4. The case statements will therefore branch at the default statement and writeline the relevent value - if it had been set to 1, 2 or 3 then the corresponding writeline will be used and the program will end. Try changing the value of i and recompile and see what happens. This can also be used with strings or characters as follows:-

import System.*;

class hello
{
	public static void main()
	{
		char c='H';

		switch(c)
		{
			case 'h':
				Console.WriteLine("The value is h");
				break;
			case 'H':
				Console.WriteLine("The value is H");
				break;
			case 'e':
				Console.WriteLine("The value is e");
				break;
			default:
				Console.WriteLine("The value isn't h, H or e");
				break;
		}
	}
}

Note that the test is case sensitive (although using toUpper or toLower would cure the problem). As I said, a very useful construct. Now on to two different concepts that may at first seem unrelated but they do lead on into the next tutorial. The first is struct. A struct (shorthand for structure) is not unlike an array in that it can contain data which can then be added to or accessed. Unfortunately, J# doesn't support it. It does however support classes which will essentially do the same thing which we will come on too later. On now to functions.

import System.*;

public class FunctionSample
{
	public static int AddItems(int Value1, int Value2) 
	{	
		int ReturnValue;

		ReturnValue=Value1 + Value2;
		return ReturnValue; 
	}


    public static void main()
    {
		int k;

		k=AddItems(1,2);
		Console.WriteLine(k);
    }
}

Functions were what made the C programming language one of the most useful. A function is a little like a subroutine except that information can be passed to it and information can be passed out of it again back to calling program. So here we have a function that will add 2 numbers and return the result of the calculation. Note that while Value1 and Value2 are part of the function decalration, it isn't necessary to declare them inside the function. However, within the function is the ReturnValue which we need to pass back to main() so it is declared.

So that's it with functions and structures. But it is possible to combine them both into...Classes...see you next time...

Back to J# Tutor

Written by Nick Cheesman. Last updated: 01/06/2007
Please eMail me at:
nickjc@nickjc.co.uk