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

    }
   
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

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