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

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Visual
AI Thread Summary
The discussion centers around correcting a C# code snippet that fails to compile due to improper class structure. Initially, the user attempted to define the `Program` class inside the `Main` method, which is not allowed in C#. The key issues identified include the need for the `static void Main` method to serve as the program's entry point and the proper placement of the `Animal` and `Dog` classes outside of the `Program` class.The corrected code structure places the `Animal` and `Dog` classes within the `ConsoleApplication5` namespace but outside the `Program` class. The `Main` method is then properly defined within the `Program` class, where an instance of the `Dog` class is created, and its `Greet` method is called. This solution resolves the compilation errors and demonstrates basic object-oriented programming principles in C#.
MathematicalPhysicist
Science Advisor
Gold Member
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.
 
Technology 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 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();
        }

    }
   
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
36
Views
3K
Replies
17
Views
2K
Replies
0
Views
2K
Replies
25
Views
2K
Replies
22
Views
3K
Replies
1
Views
3K
Replies
3
Views
4K
Back
Top