Hi,
I am just getting started (absolute beginner) as I need to learn a language for a little private project of my own, not a student (far too old) help with assignment or anything like that. I have looked around at free books videos etc and its OK but the one thing in common is that the materials tend to gloss over a lot and get you to copy code without really knowing whats going on, so you see the results of "your" work but still left confused.
I started a book C# Step by Step and it is explaining things quite nicely but there are still a few things that are not fully explained or glossed over. I don't really want to get well into the book with more and more questions going around in my head. So I would just like to check my understanding of whats going on and clarify 1 point in particular.
Its a simple simple program and I have added comments under the first method so I think I understand the flow and whats going on..
The code is::
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace DailyRate
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
public void run()
{
double dailyRate = readDouble("Enter your daily rate: ");
// Declares int dailyRate
// Assigns to METHOD readDouble ((which is text above)
// On execution goes to readDouble Method
int noOfdays = readInt("Enter the number of days: ");
writeFee(calculateFee(dailyRate, noOfdays));
}
private void writeFee(double p)
{
Console.WriteLine("The consultant's fee is {0}", p * 1.1);
}
private double calculateFee(double dailyRate, int noOfdays)
{
return dailyRate * noOfdays;
}
private int readInt(string p)
{
Console.Write(p);
string line = Console.ReadLine();
return int.Parse(line);
}
private double readDouble(string p)
{
Console.Write(p);
// Writes readDouble or p to Console
// i.e Enter your daily rate
string line = Console.ReadLine();
// this reads response to question
return double.Parse(line);
// this parses from string to double
// returns result to dailyRate and sets value
}
}
}
My query is around variable p
declared an int double dailyRate = readDouble("Enter your daily rate: ");
Being a novice I thought this was set to enter etc
But looking at the Method is looks like the value it brackets is assigned p rather than the text, it is also the same for others and it also quotes string p in intellisense.
Is this the case?
Is this always the case if I name a method()
The book mentions variable p but doesn't explain where it is so its the only thing I can think of.
My apologies if this is too basic and I seem thick (which could be true) but I just like to know what is really going on.
Thanks for reading it and if you have taken the time to respond.
No doubt the first of many.
sam
Loading
SamPosted Jan 25, 2008, 4:26 PM
Thanks for taking the time and your help
It does makes sense now, I had seen the p in the final expression, but somehow missed it in both the Console.Writeline (p) so it was mad that I couldn't see any other references.
Have a good weekend
AlanPosted Jan 25, 2008, 3:47 PM
What the following line does:
double dailyRate = readDouble("Enter your daily rate: ");
is to call the readDouble method and assign the string "Enter your daily rate: " to its parameter 'p'. The method then executes and returns a double (namely the response to the question expressed as a double) which is assigned to the variable 'dailyRate'.
A parameter is like a variable whose scope is the entire method. Like an ordinary variable, a parameter has a type so 'p' is a string and only strings can be assigned to it. So, if we now look at this line in the method:
Console.Write(p);
this writes the value of the variable 'p' (i.e. the string assigned to it) to the console.
The other methods in the program which take parameters are called and work in a similar fashion.