How do I fix this code for inheritance in visual C#?

  • Context: C# 
  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Visual
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 2K views
Messages
4,662
Reaction score
372
I have the following code which doesn't get compiled:
Code:
using System;
using System.Collections;

namespace ConsoleApplication5
{
    static void Main(string[] args) {
        class Program {
        public class Animal
        {
            public virtual void Greet()
            {
                Console.WriteLine("Hello, I'm some sort of animal!");
            }
        }
        public class Dog : Animal
        {
            public override void Greet()
            {
                Console.WriteLine("Hello, I'm a dog!");
            }
        }
    }
}
}
I used the following webapge:
http://csharp.net-tutorials.com/classes/inheritance/

Can you help me correct this code?

Thanks in advance.
 
Physics news on Phys.org
Do you have any idea yourself? Try look at the error message and then at your code.

If this doesn't help, try read up on if the Main method (or any method, for that matter) can be placed directly in a namespace.
 
  • Like
Likes   Reactions: Silicon Waffle
I don't know exactly what is causing the errors, I tried fixing this code as I have been suggested by someone on another QA site:
Code:
using System;
using System.Collections;

namespace ConsoleApplication5
{
    class Program
    {
        public class Animal
        {
            public virtual void Greet()
            {
                Console.WriteLine("Hello, I'm some sort of animal!");
            }
        }
        public class Dog : Animal
        {
            public override void Greet()
            {
                Console.WriteLine("Hello, I'm a dog!");
            }
        }
    }
}

But it seems I need to add the static void Main line, I was told to put it in the end of the code, i.e after Public class Dog : Animal {}

But it seems I need to call up the Dog class, how to do this?

Thanks.
 
MathematicalPhysicist said:
I have the following code which doesn't get compiled:
You cannot define a class inside of a method. You also need the static main method because it is the entry point of your program. It's probably a good idea to learn the basic concepts of object oriented programming.
 
Ok, I fixed it, here's the fixed code:
Code:
using System;
using System.Collections;

namespace ConsoleApplication5
{
    class Program
    {
        public class Animal
        {
            public virtual void Greet()
            {
                Console.WriteLine("Hello, I'm some sort of animal!");
            }
        }
        public class Dog : Animal
        {
            public override void Greet()
            {
                Console.WriteLine("Hello, I'm a dog!");
            }
        }
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            dog.Greet();
            Console.ReadKey();
        }

    }
   
}