ch06ex05 pg 146 problem
With only minor change to a name, i attempted to run this example. No luck. I get the error //D:\TrainingFiles\BegVCSharp\Chapter06\ch06ex05a\ch 06ex05a\ch06ex05a\Class1.cs(58): Use of unassigned local variable 'myProcess'. Any ideas? Does anyone get it to run? Here is the code:
class Class1
{
//CREATE THE DELEGATE PART -- MUCH LIKE A POINTER
delegate double ProcessDelegate(double param1, double param2);
//COUPLE OF NORMAL FUNCTIONS
static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Divide(double param1, double param2)
{
return param1 / param2;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
ProcessDelegate myProcess; //declare the delegate
Console.Write("Enter 2 numbers, separated by a comma: ");
string input = Console.ReadLine();
int commaPos = input.IndexOf(",");
double p1 = Convert.ToDouble(input.Substring(0, commaPos));
double p2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));
//SET UP THE DELEGATE TO USE
Console.Write("Enter M to Multiply or D to Divide: ");
input = Console.ReadLine().ToUpper();
//myProcess = new ProcessDelegate(Multiply);
if (input == "M") //bad form, since "q" input caused divide
{
myProcess = new ProcessDelegate(Multiply);
}
else
{
if (input == "D")
{
myProcess = new ProcessDelegate(Divide);
}
}
Console.WriteLine("Result: {0} ", (myProcess(p1, p2))); //use the delegate
//we get this error
//D:\TrainingFiles\BegVCSharp\Chapter06\ch06ex05a\ch 06ex05a\ch06ex05a\Class1.cs(58): Use of unassigned local variable 'myProcess'
Console.Read();
}
}
If you are not part of the solution, there is good money to be made by prolonging the problem.
|