Why is there a "string" inside paranthesis in a method ?
Ok so while reviewing this code which is meant to convert yards, and feet to inches I found on lines 17-19 , and 32. There are "string data" (if that makes sense) as parameters in each method? What do they denote, serve , and their purpose?
MSDN usually has convoluted explanations.....on this so I guess im resorting to you faithful guys again!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab1
{
class Lab1
{
static void Main()
{
int inches,
yards,
feet;
// this prepares to write code to give the instructions to the user
DisplayInstructions();
inches = GetInches("inches"); //why is a string(parameter) inside a method.
feet = GetInches("feet");
yards = GetInches("yards");
DisplayResults(inches, yards, feet);
}
// this method will "DisplayInstructions" to the user
public static void DisplayInstructions()
{
Console.WriteLine("This applications will allow the user to input data in yards, feet and inches " +
"& then convert that data into inches.\n");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
}
// This method will acquire the value in inches according to user input
public static int GetInches(string whichOne)
{
Console.Write("Please enter in the {0}: ", whichOne);
return int.Parse(Console.ReadLine());
}
//this method will convert a value given in yards to inches
public static int ConvertYards(int yards)
{
return yards * 36;
}
// this method will convert feet to inches
public static int ConvertFeet(int feet)
{
return feet * 12;
}
// this method will display the results of converting , inches, yards, and feet to inches
public static void DisplayResults(int inches, int yards, int feet)
{
Console.Clear();
Console.WriteLine("Inches: {0:n1}" +
"\nFeet: {1:n1}" +
"\nYards: {2:n1}" +
"\nFeet to Inches: {3:n1}" +
"\nYards to Inches: {4:n1}", inches
, feet
, yards
, ConvertFeet(feet)
, ConvertYards(yards));
Console.ReadKey();
}
}
}
Many Thanks!
techlawsam
Junior Poster in Training
56 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
The method is just badly named.
I should maybe be called GetAnnouncedUnitType(string whichOne)
The parentheses let you know the string is being passed as a parameter to the method.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
techlawsam
Junior Poster in Training
56 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Yes, you should name methods so they have a common sence. And every one can understands them. The best approach is to name them in this kind of manner, that someone who reads it, even approximatelly knows what is inside the method (the code it self).
Rename this method (GetInches) to something common to all the "dimmension common langauge", like GetUnitType() as thines proposed.
Btw, there is one more thing, if user enters a non integer value, you need to have some kind of a checking, to avoid exceprtions, like:
// This method will acquire the value in inches according to user input
public static int GetUnitType(string whichOne)
{
Console.Write("Please enter in the {0}: ", whichOne);
int intValue = 0;
while(true)
{
if(!int.TryParse(Console.ReadLine(), ont intValue))
Console.WriteLine("Wrong format. Please re-type it.");
else
break;
}
return int.Parse(Console.ReadLine());
}
Hope it helps,
bye
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474