• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Error when inheriting from a class

Hello

I get this compile-time error with this console application "No overload for method 'Customer' takes '0' arguments". This is on the line "public class Mobile60 : Customer". The Mobile60 appears underlined in blue in Visual Studio. Why?

using System;

namespace MortimerPhones
{
      public enum TypeOfCall
      {
            ...............
      }

      public class Customer
      {
            protected string name;
            protected decimal balance;

            public string Name
            {
                  get
                  {
                        return name;
                  }
                  set
                  {
                        name = value;
                  }
            }

            public decimal Balance
            {
                  get
                  {
                        return balance;
                  }
            }

            public void RecordPayment(decimal amount)
            {
                  balance -= amount;
            }

            public virtual void RecordCall(TypeOfCall calltype, uint minutes)
            {
                  switch (calltype)
                  {
                        case TypeOfCall.CalltoLandline:
                              balance += 0.02M * minutes;
                              break;
                        case TypeOfCall.CalltoMobile:
                              balance += 0.3M * minutes;
                              break;
                  }
                  
            }
      }

      public class Mobile60 : Customer
      {
            public override void RecordCall(TypeOfCall calltype, uint minutes)
            {
                  uint lowcostmins,highcostminsused;
                  switch (calltype)
                  {
                        ............
                                                }      
            }
      }
   
      class tester
      {
            public static void Main()
            {
                    ..............
                                }
      }
}
0
Kokas79
Asked:
Kokas79
  • 4
  • 3
2 Solutions
 
RomanPetrenkoCommented:
On which line the error happened?
0
 
Kokas79Author Commented:
public class Mobile60 : Customer
0
 
RomanPetrenkoCommented:
such code compiled fine:
===
using System;

namespace MortimerPhones
{
      public enum TypeOfCall
      {
            CalltoLandline,
            CalltoMobile
      }

      public class Customer
      {
            protected string name;
            protected decimal balance;

            public string Name
            {
                  get
                  {
                        return name;
                  }
                  set
                  {
                        name = value;
                  }
            }

            public decimal Balance
            {
                  get
                  {
                        return balance;
                  }
            }

            public void RecordPayment(decimal amount)
            {
                  balance -= amount;
            }

            public virtual void RecordCall(TypeOfCall calltype, uint minutes)
            {
                  switch (calltype)
                  {
                        case TypeOfCall.CalltoLandline:
                              balance += 0.02M * minutes;
                              break;
                        case TypeOfCall.CalltoMobile:
                              balance += 0.3M * minutes;
                              break;
                  }
               
            }
      }

      public class Mobile60 : Customer
      {
            public override void RecordCall(TypeOfCall calltype, uint minutes)
            {
                  uint lowcostmins,highcostminsused;
                  switch (calltype)
                  {
                        case TypeOfCall.CalltoLandline:
                              break;
                  }    
            }
      }
   
}
===

So I thinks the problem in the code you didn't show here. May be you can post whole code?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kokas79Author Commented:
using System;

namespace MortimerPhones
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      ///
      public enum TypeOfCall
      {
            CalltoMobile, CalltoLandline
      }

      public class Customer
      {
            protected string name;
            protected decimal balance;

            public Customer(string name)
            {
                  this.Name = name;
            }

            public string Name
            {
                  get
                  {
                        return name;
                  }
                  set
                  {
                        if (value.Length > 3 && value.Length <= 6)
                        {
                              name = value;
                              return;
                        }
                        else
                        {
                              Console.WriteLine("The name must be between 3 and 6 characters.");
                              return;
                        }
                  }
            }

            public decimal Balance
            {
                  get
                  {
                        return balance;
                  }
            }

            public void RecordPayment(decimal amount)
            {
                  if (Balance >= amount)
                  {
                        balance -= amount;
                        Console.WriteLine("Thank you for your payment. Your new balance is £{0}",balance);
                        return;
                  }
                  else
                  {
                        Console.WriteLine("You cannot pay more than your balance!");
                        return;
                  }
            }

            public virtual void RecordCall(TypeOfCall calltype, uint minutes)
            {
                  switch (calltype)
                  {
                        case TypeOfCall.CalltoLandline:
                              balance += 0.02M * minutes;
                              break;
                        case TypeOfCall.CalltoMobile:
                              balance += 0.3M * minutes;
                              break;
                        default:
                              break;
                  }
                  Console.WriteLine("{0}: your new balance is {1}",Name, Balance);
            }
      }

      public class Mobile60 : Customer
      {
            public override void RecordCall(TypeOfCall calltype, uint minutes)
            {
                  uint lowcostmins,highcostminsused;
                  switch (calltype)
                  {
                        case TypeOfCall.CalltoLandline:
                              balance += 0.02M * minutes;
                              break;
                        case TypeOfCall.CalltoMobile:
                              highcostminsused = minutes > 60? 60 : minutes;
                              if (minutes > 60)
                              {
                                    lowcostmins = minutes - 60;
                              }
                              else
                              {
                                    lowcostmins = 0;
                              }
                              balance += highcostminsused * 0.5M + lowcostmins * 0.1M;
                              break;
                        default:
                              break;
                  }
                  Console.WriteLine("{0}: your new balance is {1}",Name,Balance);
            }
      }
   
      class tester
      {
            public static void Main()
            {
                  Customer kokas = new Mobile60();
                  Customer mimis = new Customer("Mimis");
                  mimis.RecordCall(TypeOfCall.CalltoMobile,1);
                  mimis.RecordCall(TypeOfCall.CalltoLandline,10);
                  mimis.RecordPayment(0.5M);
            Console.ReadLine();
            }
      }
}
0
 
RomanPetrenkoCommented:
Here is your problem:
Customer class has contructor with 1 parameter
When you inherit Mobile60 class you define how constructor of parent will be called:
public class Mobile60 : Customer
      {
            public Mobile60():base(""){}
.........
      }

or you have to declare another constructor in Customer - without parameters.
public class Customer
{
      protected string name;
      protected decimal balance;
      public Customer()
      {
            this.Name = "";
      }
................
}
0
 
YurichCommented:
that's your problem:

you don't have a default constructor in your Customer class (with no parameters), and since you have specified one on your own with string name as a parameter, the compiler will not provide the default one for you, neither do you have a default constructor in your Mobile60 class:

public Customer(string name)
{
   this.Name = name;
}

Yet, you're trying to call your class with no parameters:
               Customer kokas = new Mobile60();

and absolutely logically logically getting an error.

Just provide a default constructor in your Customer class and will compile just fine:
public Customer()
{
}

that's it!

regards



0
 
RomanPetrenkoCommented:
or declare
public class Mobile60 : Customer
     {
          public Mobile60(string name):base(name){}
..........
     }

and change main()

public static void Main()
{
  Customer kokas = new Mobile60("kokas");
  ............
}
0
 
Kokas79Author Commented:
I'm such an amateur! Thanks guys :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now