如何调用类中的方法

时间:2014-05-05 17:44:09

标签: c# arrays class methods

自己学习C#(不是作业)。我编写了一个TotalDue方法来计算到期的所有客户余额(来自数组)。将它放在Customer类中,以便它可以访问数据。我无法弄清楚如何在main中调用此方法。如何显示总数?

class Program
{
    static void Main(string[] args)
    {
        Customer[] customers = new Customer[2];
        string customer;
        int id;
        double due;

        // GET DATA AND FILL ARRAY
        for (int x = 0; x < customers.Length; ++x)
        {
            GetData(out customer, out id, out due);
            customers[x] = new Customer(customer, id, due);
        }

        // SORT ARRAY - NEEDS ICOMPARABLE<Customer> - PART 1
        Array.Sort(customers);


        // PRINT ARRAY WITH TOSTRING() OVERRIDE
        for (int x = 0; x < customers.Length; ++x)
        {
            Console.WriteLine(customers[x].ToString());
        }

        //DON'T KNOW HOW TO CALL THE TOTAL DUE METHOD...


        Console.ReadLine();
    }

    class Customer : IComparable<Customer>  // SORT ARRAY - PART 2
    {
        private string CustomerName { get; set; }
        private int IdNumber { get; set; }
        private double BalanceDue { get; set; }

        // CONSTRUCTOR
        public Customer(string customer, int id, double due)
        {
            CustomerName = customer;
            IdNumber = id;
            BalanceDue = due;
        }

        //SORT THE ARRAY - PART 3
        public int CompareTo(Customer x)
        {
            return this.IdNumber.CompareTo(x.IdNumber);
        }

        // OVERRIDE TOSTRING TO INCLUDE ALL INFO + TOTAL
        public override string ToString()
        {
            return ("\nCustomer: " + CustomerName + "\nID Number: " + IdNumber + "\nBalance Due: " + BalanceDue.ToString("C2"));
        }

        // TOTAL DUE FOR ALL CUSTOMERS
        static void TotalDue(Customer [] customers)
        {
            double Total = 0;

            for (int x = 0; x < customers.Length; ++x)
                Total += customers[x].BalanceDue;

            Console.WriteLine("Total Amount Due:  {0}", Total.ToString("C2"));
        }
    }

    // GET DATA METHOD
    static void GetData(out string customer, out int id, out double due)
    {
        Console.Write("Please enter Customer Name:  ");
        customer = Console.ReadLine();
        Console.Write("Please enter ID Number:  ");
        int.TryParse(Console.ReadLine(), out id);
        Console.Write("Please enter Balance Due:  $");
        double.TryParse(Console.ReadLine(), out due);
    }

}

4 个答案:

答案 0 :(得分:2)

TotalDue方法设为公共,因为C#中的默认访问修饰符是私有的,然后尝试此操作。

class Customer : IComparable<Customer>  // SORT ARRAY - PART 2
{
     public static void TotalDue(Customer [] customers)
     {
        double Total = 0;

        for (int x = 0; x < customers.Length; ++x)
            Total += customers[x].BalanceDue;

        Console.WriteLine("Total Amount Due:  {0}", Total.ToString("C2"));
     }
}

static void Main(string[] args)
{
    // ...........
    //............
     Customer.TotalDue(customers);
}

答案 1 :(得分:0)

C#中的默认访问修饰符为private。由于您的TotalDue方法未指定任何其他内容,因此它为private。您可以将其更改为public,然后通过Main

进行调用

答案 2 :(得分:0)

使用静态方法添加公共访问修饰符并使用类名称调用它或删除static关键字并使其成为实例并调用类方法。

答案 3 :(得分:-1)

使您的Customer类方法(TotalDue)公开而非静态...