Lesson 3.2 Void Method with Parameters
Sometimes, some jobs are not just a simple matter of telling someone to do something. Sometimes, there are questions that need answering, things the performer of the job will need to know to accomplish what you have set them to.
A classic example is asking someone for coffee. Everyone has a preference: “White two sugars”, “black no sugar” etc. Even if they don’t like coffee, it’s a preference. You are asking for a job to be accomplished and providing parameters for how you’d like it done. If you think about, that’s how the majority of your requests are carried out unless it’s a simple request of “pass the salt”, “tell me how you’re feeling” or “get down, he’s got a gun”.
C# also emulates this; you’ll find that you generally use a parameter version more often than void. To illustrate how parameters can be passed to a method, consider the timing program from the last example. It placed a time stamp on the output console, very nice. But we still need to output manually whether that’s the start time or the end time.
System.Console.WriteLine("Start Time:");
ShowTimeStamp();
Wouldn’t it be nice if we could tell the ShowTimeStamp method what to write just before the time stamp, a preamble if you like?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Methods3
{
class Program
{
static void Main(string[] args)
{
ShowTimeStamp("Start Time:");
for (int x = 0; x <= 100000000; x++)
{
}
ShowTimeStamp("End Time:");
System.Console.ReadKey(true);
}
static void ShowTimeStamp(string preamble)
{
System.Console.WriteLine(preamble);
System.Console.Write("*************");
System.Console.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));
System.Console.WriteLine("*************");
}
}
}
And there you are: A version of the method that takes a parameter. It’s quite similar to the last one. There are three main differences.
First change is the change in the declaration of the ShowTimeStamp method. It actually has something in the brackets now. The brackets are for containing parameters. If there are none to be passed or taken then the brackets stay empty.
static void ShowTimeStamp(string preamble)
It asks for a string variable to be passed in, the actual name of the parameter is arbitrary. You could, like most names in C#, have pretty much called it what you wanted. I have called it preamble as that is what it is going to be used as.
The second change is that I have added a line to the method that actually uses the preamble that is passed in.
static void ShowTimeStamp(string preamble)
{
System.Console.WriteLine(preamble);
System.Console.Write("*************");
System.Console.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"));
System.Console.WriteLine("*************");
}
It simply takes that string and writes it to the console before it writes the time stamp.
The third, and last, change is where I altered the calls to that method.
static void Main(string[] args)
{
ShowTimeStamp("Start Time:");
for (int x = 0; x <= 100000000; x++)
{
}
ShowTimeStamp("End Time:");
System.Console.ReadKey(true);
}
I have passed a simple string that is to be written before the time stamp. It should be easy to see what the text is for. Note again that running the new program will give you exactly the same response as the previous one; yet more refactoring afoot.