C Programming Yellow Book
6019BjHWX
6019BjHWX
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Simple Data Processing<br />
A First C# Program<br />
()<br />
A method call is followed by the parameters to the method. A parameter is something<br />
that is passed into a method for it to work on. Think of it as raw materials for a process<br />
of some kind. In the case of ReadLine it has no raw materials; it is going to fetch the<br />
information from the user who will type it in. However, we still have to provide the list<br />
of parameters even if it is empty.<br />
;<br />
We have seen the semi-colon before. It marks the end of a statement in our program.<br />
width =<br />
This is another assignment (Mr. Phelps/Hawke 1 ). The variable width is being given a<br />
value. Many statements in your programs will simply be moving data around and<br />
performing actions on it.<br />
double.<br />
But the rest of the statement looks a bit scary. In fact it is not too tricky. We are asking<br />
Mr. double (the thing responsible holding for double precision floating point<br />
numbers) to do a little job for us. In this case the little job is "take the string held by<br />
widthString and convert it into a double precision floating point number". Mr.<br />
double provides this ability by exposing a method called Parse.<br />
Note that there is nothing wrong or naughty about something in C# exposing its<br />
methods. It is how we get things done for us. When you come to design larger<br />
programs you will find that the best way to do this is to create components which<br />
expose methods to get the job done. The whole of the C# library set is provided as a<br />
series of methods and one of the things that you will have to get to grips with is where<br />
the methods are and how to use them. As with many things in life, the trick is knowing<br />
who to ask…<br />
Parse<br />
The Parse method has the job of converting the string it has been given into a double<br />
precision floating point number. To do this it must look along the string, pull out each<br />
digit in turn and then calculate the actual value, as in "12" means a ten and two units.<br />
This process of looking along things is often called parsing. Hence the name of the<br />
method we are using. The method is given the string that is to be parsed and returns the<br />
number that it has found.<br />
Note that this gives significant potential for evil, in that if the user doesn’t type in a<br />
value or types something like "Twenty Five" the Parse method call will not be able to<br />
resolve a number and will fail as a result. How it fails, and how you can resolve this<br />
failure, will be left for a future section in order to add more excitement to this text.<br />
(widthString);<br />
1<br />
Obscure movie/TV show reference for you there folks.<br />
We have seen that a call of a method must be followed by the raw materials<br />
(parameters) for that method. In the case of ReadLine there are no parameters, but we<br />
still need to supply an empty list to indicate this. In the case of Parse the method<br />
needs to be given the string that it is to work on. We do this by putting the name of the<br />
string variable containing the text (widthString) into the brackets as above. The<br />
value of the information in widthString (i.e. the text that the user has typed in) is<br />
passed into the Parse method for it to work on and extract the number from.<br />
C# <strong>Programming</strong> © Rob Miles 2015 18