am starting to learn use delegates ,but am having a problem to solve this code ,can u help , solve it and say what am doing wrong, thanks.
here is what i did.

namespace DelegateHello
{
class Program
{
public delegate void Hello()


static void Main(string[] args)
{
Hello greetings = null;


Hello g1 = new Hello(Good Morning);
Hello += g1;


Hello g2 = new Hello(Good Evening)
Hello += g2;
}


public static void Goodmorning()
{
Console.WriteLine(" Good Morning "+g1);
}


public static void Goodevening()
{
Console.WriteLine(" Good Evening"+g2);
}


}
}

Your function Goodmorning is referring to some variable g1, and there is no variable named g1 in its scope. Learn how scope works in C# and you'll understand your problem.

Edit: And I'm assuming all the typos happened because you typed this manually, and you're not actually having problems caused by extra spaces and incorrect capitalization.

am more confused now, u mean
i should make declarations of my variables first. am i rigth

In your code sample, g1 and g2 are local variables in the Main function. They cannot be seen anywhere outside the main function. Since you're trying to use a variable named g1 in GoodMorning, you're getting an error.

Hopefully this will make more sense to you.
You declare a delegate method with no parameters. You want to assign two instances of this delegate and each will be assigned to a seperate method.

Each method must have the same parameter list as the delegate (which you do have).

Your attempt at assigning an event (+=) is totally wrong. Don't confuse event handlers with delegates. Delegates basically define a method call syntax. You can assign any method that has the same list of parameters to an instance of the delegate.

In the code snipet, I create an instance of Hello to a variable named g1. The delegate takes a single parameter which is the name of the method that will be called.

Then you invoke (call) the instance to cause the assigned method to be executed.

public delegate void Hello();
        public delegate void Hello2(string value);

        static void Main(string[] args)
        {
            // Assign g1 to the Goodmorning() method
            Hello g1 = new Hello(Goodmorning);
            // Assign g2 to the Goodevening() method
            Hello g2 = new Hello(Goodevening);
            Hello2 g3 = new Hello2(Salutations);
            // Invoke both methods through the delegate
            g1();
            g2();
            g3("Good Afternoon");
            // Wait for the user to press a key to exit
            Console.WriteLine("<press any key to exit>");
            Console.ReadKey();
        }

        public static void Goodmorning()
        {
           Console.WriteLine("Good Morning");
        }

        public static void Goodevening()
        {
            Console.WriteLine("Good Evening");
        }
        public static void Salutations(string value)
        {
            Console.WriteLine(value);
        }
commented: Good explanation! +4

Thank you all for the help.
its good discusing with you all and we the beginners become more comfortable with the computer tech.
my children love when we solve such little tasks with the computer on visual studio .
thanks guys jerryShaw and Rashikal Fol.
bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.