1

I am new to C#. I have a Persons class with this function:

public virtual void InputPerson(Persons P)
    {           
        P.ID = int.Parse(Console.ReadLine());
        Console.WriteLine("Titel:");
        P.Titel = Console.ReadLine();
        Console.WriteLine("Name:");
        P.Name = Console.ReadLine();
        Console.WriteLine("Surname:");
        P.Surname = Console.ReadLine();         
    }

And I have a User class that inherits form this Persons class. I now need to create a InputUser function in the User class that makes use of this InputPerson function in the Persons class without rewriting all the code from the InputPerson function to the InputUser function. Here is my code from the InputUser function:

public override void InputPerson(User U)
    {
        Console.WriteLine("Please enter a Customer:");
        Console.WriteLine("Customer ID:");
        base.InputPerson;
        Console.WriteLine("Telephone Number:");
        U.Telephone = int.Parse(Console.ReadLine());

        Console.WriteLine();
    }

This InputUser code gives me a error stating:

'UserCustomerNotes.User.InputPerson(UserCustomerNotes.User)': no suitable method found to override

Can anyone please help?

Thanks in advance

0

5 Answers 5

2
  1. base.InputPerson; cannot work at all.

  2. You cannot override void InputPerson(Persons P) with void InputPerson(User U) as you seem to imply. The signatures must be identical.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I did thought that this is my problem. Thank you.
1

This is just me taking more tangential look at this question but under the traditional guidelines for OO design a user is a person therefore you get inheritance. However, when we look at SOLID principles and especially the "Liskov Substitution Principle":

"Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it." — Robert Martin, LSP paper linked from The Principles of OOD

What does that mean? Well, we should be able to substitute an instance of a subclass for its parent class and everything should continue to work. In this case it won't work, there are extra properties that you are depending on for User that a Person does not have.

In this case have you considered doing composition rather than inheritance?

public class Person
{
   public static void InputPerson(Person p)
   {
      // Do the input logic here
   }
}

public class User
{
    public Person Person { get; private set; }

    public static void InputUser(User u)
    {
        if (u.Person == null)
            u.Person = new Person;
        Person.InputPerson(u.Person);
        Console.WriteLine("Telephone:");
        u.Telephone = Console.ReadLine();
    }
}

We could go further into SOLID and take a look at the Single Responsibility Principle and ask is the Person/User object handles the logic of being a person but should it know about creating that object from the console? What happens if you want to re-use the same logic for a windows application or a WPF application?

Comments

0

To override a method you must keep its signature, which you dont(you change the type of the parameter from Persons to User, so you create a new method). So remove the 'override' keyword.

Also, use:

InputPerson(U);

Instead of:

base.InputPerson;

Comments

0

You could also do the following, taking advantage of the class inheritance:

public override void InputPerson(Persons P)
{
   // Code in here
}

Because a User is a Persons, then you are allowed to directly pass it into the Method as a parameter. If however there are custom Properties on a User that are not present on a Persons you could introduce generics.

public class Persons<T>
{
   public virtual void InputPerson(T p)
   {
 // code here.
   }
}

public class User : Person<User>
{
 public override void InputPerson(User p)
 {
  // code here. You can now treat the input as a user
  // as you have told your base class that the 'T' is a user.
 }
}

1 Comment

and what would the other uses of Person<User> be? you'd be unable to substitute a Person<FootballPlayer> for a Person<User> whereas if they derived from Person you could make that substitution. What I'm saying is that you are making it more complex with out gaining anything (you are basically destroying the inheritance/relationship) between different kinds of persons
-1

Instead of overriding the method, use the new keyword instead as the signatures are different.

public new void InputPerson(User U)

Then all you need to do is pass User U through to the base class, so instead of

base.InputPerson;

use

base.InputPerson(U);

4 Comments

No, he's not a live saver, as you will end up with an illogically constructed program and won't learn anything but how to fix compilation errors.
@Ondrej: But will it not be a far better learning experience than to simply be told to do things another way, with no true understanding as to why? You can't learn everything at once
but you didn't even take the time to explain why the solution you are suggesting is usually seen as a strong smell. So instead of helping OP you encouraged to use bad practice (without even give a hit to the pit you could be leading her/him into)
@Rune FS: The "Hi, I am new to C#" told me that they are just starting to learn the language, and I didn't want to confuse them with aspects of OO design they were probably not ready for, but maybe this was wrong. If you would like to explain to them why this is bad practise though I am sure they would be more than grateful?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.