Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error 1 Expected class, delegate, enum, interface, or struct Program.cs 20 12
Error 'void'
|
\/
static void ProcessAndDisplayNumber(DoubleOp action, double value)
{
Console.WriteLine("\nProcessAndDisplayNumber called with value = " + value);
action(value);
}

class Program
{
static void Main(string[] args)
{
DoubleOp operations = new DoubleOp(MathOperations.MultiplyByTwo);
operations += new DoubleOp(MathOperations.Squre);
ProcessAndDisplayNumber(operations, 2.0);
ProcessAndDisplayNumber(operations, 7.94);
ProcessAndDisplayNumber(operations, 1.414);
Console.WriteLine();
}
}
Posted
Comments
Sandeep Mewara 5-Aug-10 14:00pm    
Don't post more errors as answers. Instead try what others have suggested. If you face issue while doing them, use 'Add Comment' feature to respond to that answer.

Put the code inside class, struct ...,

for example

class mySuperClassName
{
}
 
Share this answer
 
C#
static void ProcessAndDisplayNumber(DoubleOp action, double value)
{
Console.WriteLine("\nProcessAndDisplayNumber called with value = " + value);
action(value);
}


Has to be moved in side your

C#
class Program
{
...
}


Can change class Program to static class Program
 
Share this answer
 
You have modified and broken sample code from the book Professional C# 2008 published by Wrox. Why don't you just go back to the original to see what you have done wrong?

It available here Sample code download[^]

For your convenience here is the code that you have broken.

C#
using System;
using System.Collections.Generic;
using System.Text;

namespace Wrox.ProCSharp.Delegates
{
   class MathOperations
   {
      public static double MultiplyByTwo(double value)
      {
         return value * 2;
      }

      public static double Square(double value)
      {
         return value * value;
      }
   }


   delegate double DoubleOp(double x);

   class MainEntryPoint
   {
      static void Main()
      {
         DoubleOp[] operations =
            {
               MathOperations.MultiplyByTwo,
               MathOperations.Square
               //new DoubleOp(MathOperations.MultiplyByTwo),
               //new DoubleOp(MathOperations.Square)
            };

         for (int i = 0; i < operations.Length; i++)
         {
            Console.WriteLine("Using operations[{0}]:", i);
            ProcessAndDisplayNumber(operations[i], 2.0);
            ProcessAndDisplayNumber(operations[i], 7.94);
            ProcessAndDisplayNumber(operations[i], 1.414);
            Console.WriteLine();
         }
      }

      static void ProcessAndDisplayNumber(DoubleOp action, double value)
      {
         double result = action(value);
         Console.WriteLine(
            "Value is {0}, result of operation is {1}", value, result);
      }
   }
}



Alan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900