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
Click For Summary

Discussion Overview

The thread discusses issues related to inheritance in C# code, specifically focusing on the placement of classes and the Main method within a namespace. Participants seek to correct compilation errors in the provided code snippets.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents initial code that fails to compile, indicating the need for corrections.
  • Another participant suggests examining the error message and the code structure, particularly the placement of the Main method.
  • A different participant expresses uncertainty about the errors and seeks clarification on where to place the static Main method.
  • One participant emphasizes that classes cannot be defined inside methods and reiterates the necessity of the static Main method as the program's entry point.
  • Another participant provides a reference to a "Hello World" example, advising on the proper structure of the classes and the instantiation of objects.
  • A later reply shares a corrected version of the code, demonstrating the proper placement of the classes and the Main method, along with instantiation of the Dog class.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper class structure and the placement of the Main method, but there is some uncertainty regarding the specifics of implementation and the initial errors encountered.

Contextual Notes

There are unresolved questions about the exact nature of the compilation errors and the assumptions made regarding the structure of C# programs.

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

    }
   
}
 

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 0 ·
Replies
0
Views
3K
Replies
3
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K