-1

Good day, i am pretty new to how delegates work and require some assistance here is my code so far this is code in my main class.

using System;
using System.Collections.Generic;
using System.Linq;

namespace PROG6211_Taks_1_Tarisai_Gonah_18028480
{

    class Program
    {

        public void notify(double exp)
        {
            Calculate cal1 = new Calculate();

            cal1.totalExpenses = cal1.totalExpense + (cal1.carpayMonth + cal1.repayMonth);

            if ((cal1.totalExpenses / cal1.grossIncome) * 100 > 75)
            {
                Console.WriteLine("Your expenses exceed 75% of gross Income");
            }

        }

        static void Main(string[] args)
        {
            Calculate cal2 = new Calculate();

            cal2.expenses = new cal2.expenses(notify);

             var mydelegate = new expenses(cal2.notify);

In my main class i am trying to call trying to make the delegate run but im am struggling with that i am failing to create a method for it. here is my other class:

public delegate void expenses(double totalExpenses);
public  void notify(expenses TotalExpenses)
{

    TotalExpenses(0);

    totalExpenses = totalExpense + (carpayMonth + repayMonth);

    if ((totalExpenses / grossIncome) * 100 > 75)
    {
        Console.WriteLine("Your expenses exceed 75% of gross Income");

    }

So I have created a method that does the function I require I did not show the full code because it's too much code but I have no issue with that, my problem is making the delegate in my other class run in my main method.

ive tried using this structure:

MyClass myClass = new MyClass();

MyClass.delgatename object = new MyClass.delegatename(method);

myClass.methodinotherclass(object); 

this is the best i can to describe my problem hope you understand :)

1 Answers1

0

You can declare the delegate type outside your class if possible. sort of like below -

public delegate void Expenses(double totalExpenses);
public class MyFamily
{
    public void ExpensesImplement(double t)
    {
        Console.WriteLine(t);
    }
}
public class MyOtherFamily
{
    public new void ImplementExpenses(double t)
    {
        Console.WriteLine(t);
    }
}
public class GFG
{
        // Main method
        static public void Main()
        {
            MyFamily mf = new MyFamily();
            MyOtherFamily mm = new MyOtherFamily();
            Expenses expenses = new Expenses(mf.ExpensesImplement);
            expenses += mm.ImplementExpenses;
            expenses(10);
       }
    }
}
Aadittya
  • 59
  • 6