3

I am new to C#, and are trying to understand classes and "is a" relationships. I've been trying to get a piece of code to work, but I can't seem to get it right.

It looks like this: http://pastebin.com/cQDusptB

I have a base class, Mammal, which one should be able to create instances of, giving the name of the mammal as input. I also have a class, Dog, which is a Mammal. One should be able to create an instance of this too, in the same manner as Mammal.

Can anyone see my flaw(s)? If yes, can you please explain what i misunderstood/forgot?

1
  • You can include a small example of the problem you're having right in your question. Pastebin not required. Commented Mar 4, 2012 at 15:47

4 Answers 4

6

When posting questions like this it is helpful to also post the error message the compiler gave you.

Can anyone see my flaw(s)?

You have a virtual constructor.

can you please explain what i misunderstood/forgot?

Constructors are not inherited in C# and therefore it makes no sense to mark them as either virtual or override.

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

Comments

3

The constructor of Dog needs to be named "Dog". You can't/don't need to override "Mammal". Also, Mammal's constructor shouldn't be virtual.

public class Mammal 
{

    public Mammal(string name)
    {
        //....
    }
}

public class Dog : Mammal {

    public Dog(string name) : base(name) 
    {
    }
}

You pass arguments to the base class constructor using the "base" keyword (see code above).

Be sure to pick up a good book on C# to get the language basics right.

1 Comment

Thank you. This makes sense :) - I have a book, but I didn't quite get its explanation of this part :(
1

You have a few issues here. For one, constructors can not be marked as virtual or overridden. Another problem is that you attempt to call the method .Name without parentheses. The corrected version looks like this:

public class Mammal
{
    protected string _Name;

    public virtual string Name()
    {
            return (this._Name + " - of type " + this.GetType());
    }
    public Mammal(string Name)
    {
        this._Name = Name;
    }
}
public class Dog : Mammal
{
    public Dog(string Name) : base(Name)
    {
    }

    public override string Name()
    {
        return (base._Name + "Dog");
    }
}

static void Main()
{
    Mammal AnimalA = new Mammal("SubjectA");
    Console.WriteLine("{0}", AnimalA.Name());

    Mammal AnimalB = new Dog("SubjectB");
    Console.WriteLine("{0}", AnimalB.Name());
    Console.ReadLine();
}

2 Comments

Thanks! - I understand the changes! - I'm wondering though, is there any easy workaround, if i wanted to just call .Name without the () ?
1

Constructors are not inherited/overridden.

  • Lose the 'virtual' keyword from the Mammal constructor in the Mammal class.
  • Remove the Mammal ctor override attempt in the Dog class.
  • Replace it with a Dog constructor that calls the Mammal one (like this):

public Dog(string name) : base(name) {

}

Now when a dog is constructed it will call through to the mammal base class constructor passing the specified name.

Comments

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.