Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Inheritance in visual C#.

  1. Jan 17, 2016 #1

    MathematicalPhysicist

    User Avatar
    Gold Member

    I have the following code which doesn't get compiled:
    Code (C#):

    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.
     
  2. jcsd
  3. Jan 17, 2016 #2

    Filip Larsen

    User Avatar
    Gold Member

    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.
     
  4. Jan 17, 2016 #3

    MathematicalPhysicist

    User Avatar
    Gold Member

    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 (C#):

    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.
     
  5. Jan 17, 2016 #4
    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.
     
  6. Jan 17, 2016 #5

    jedishrfu

    Staff: Mentor

    Look at the hello world example:

    http://csharp.net-tutorials.com/basics/hello-world/

    Place the Dog and Animal classes after or before the Program class not inside the program class then in the Program class main method add the code to instantiate a Dog object or whatever you want to do.
     
  7. Jan 17, 2016 #6

    MathematicalPhysicist

    User Avatar
    Gold Member

    Ok, I fixed it, here's the fixed code:
    Code (C#):

    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();
            }

        }
       
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Inheritance in visual C#.
  1. C++ and Visual C++ (Replies: 4)

  2. New to Visual C++ (Replies: 11)

Loading...