-1

Type definitions

using System;
    
public enum Direction { Right, Left, Forward };

class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;

    public Chaharpa(int a, int b, int x, int y)
    {
        age = a;
        height = b;
        cordinates_x = x;
        cordinates_y = y;
    }

    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }

    public int Age
    {
        get { return age; }
    }

    public int Height
    {
        get { return height; }
    }

    public int Cordinates_x
    {
        get { return cordinates_x; }
        set { cordinates_x = value; }
    }

    public int Cordinates_y
    {
        get { return cordinates_y; }
        set { if (value > 0) cordinates_y = value; }
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }

    class horse : Chaharpa
    {
        public bool is_wild;

        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;
        }

        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
    }
}

Usage

class Program
{
    static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;

        for (int i = 0; i < 3; i++)
        {

            Console.WriteLine("Horse #" + (i + 1));

            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());

            minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }

        Console.WriteLine();

        horse ob = new horse();

        ob.Jump(minAge);

        ob.move();

        ob.Print();

        Console.ReadKey();
    }
}

I get these errors in Visual Studio:

'Chaharpa' does not contain a constructor that takes 0 arguments

The type or namespace name 'horse' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'horse' could not be found (are you missing a using directive or an assembly reference?)

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 3
    Class `horse` is _nested_ inside class `Chaharpa` and it is not public. So, it is not visible in Main. Make it a public first-level class (for starters). Then you derive `horse` (which should be `Horse`) from Chaharpa, without specifying a CTOR. That's why a default ctor will be created for you, which will try to call its parent's (default) ctor, which does not exist, because you defined two parameterized ctors. – Fildor Aug 25 '20 at 07:21
  • `Chaharpa` doesn't have a constructor that takes 0 arguments. It has constructors that take 2 or 4 arguments. The `horse` class has to call one of those, but doesn't even have its own constructor – Panagiotis Kanavos Aug 25 '20 at 07:22
  • If you don’t declare any constructors in a class (horse class), the compiler automatically provides a public parameter less constructor (there is no parameter less constructor is the Chaharpa class). – MatteoCracco97 Aug 25 '20 at 07:23
  • Naming conventions for c#: PublicThingsPascalCase, nonPublicThingsCamelCase, avoid underscores in names containing lowercase letters – Caius Jard Aug 25 '20 at 07:31
  • This is request. The horse class is wrong. – Denem Nettsi test Aug 25 '20 at 08:20

2 Answers2

0
  1. In the class Chaharpa you defined two constructors, both take arguments. Creating your own constructor overrides the default constructor. Usually, when inheriting from a base class you want to initialize the inheriting class with parameters that are used to initialize the base class, look more at this thread.

  2. The horse class inside Chaharpa and Chaharpaclass are not public. Changing classes horse and Chaharpa to public and accessing it as: Chaharpa.horse ob = new Chaharpa.horse(); is the right way to go.

Here are some mitigations to the code:

using System;
using System.ComponentModel;

public enum Direction
{ Right, Left, Forward };
public class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;
    public Chaharpa()
    {
    }

    public Chaharpa(int a, int b, int x, int y)
    {
        age = a;
        height = b;
        cordinates_x = x;
        cordinates_y = y;
    }
    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }
    public int Age
    {
        get { return age; }
    }

    public int Height
    {
        get { return height; }
    }

    public int Cordinates_x
    {
        get { return cordinates_x; }
        set { cordinates_x = value; }
    }
    public int Cordinates_y
    {
        get { return cordinates_y; }
        set { if (value > 0) cordinates_y = value; }
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }
    public class horse : Chaharpa
    {
        public bool is_wild;

        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;        
        }

        public void move()
        {
            throw new NotImplementedException();
        }

        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;

        for (int i = 0; i < 3; i++)
        {

            Console.WriteLine("Horse #" + (i + 1));

            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());

            minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }
        Console.WriteLine();

        Chaharpa.horse ob = new Chaharpa.horse();

        ob.Jump(minAge);
                
        // You can call the Move defined in Chaharpa
        ob.Move(<PASS DIRECTION PARAMETER HERE>);

        ob.Print();

        Console.ReadKey();

    }
}
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
  • 3
    Using one file per class is *not* a best practice, it's just a common code organization practice. When the classes are small, it makes perfect sense to put related definitions in the same file. Now that C# 9 records allow defining a class with a single line, expect that to be a lot more common – Panagiotis Kanavos Aug 25 '20 at 07:24
  • In fact, if you check .NET's own Github repository you'll see that *many* files contain multiple tightly-related classes – Panagiotis Kanavos Aug 25 '20 at 07:25
  • 2
    Not nesting classes at all is probably a better way to go – Caius Jard Aug 25 '20 at 07:26
  • @PanagiotisKanavos I agree that it is OK and even sometimes helpful to put classes in the same file in C#. I want to give the OP some advice on how to better organize it's code, as the main program class on the defined object classes 'Chaharpa' and 'horse' - definitely shouldn't reside on the same file. – Aviv Yaniv Aug 25 '20 at 07:31
0

First, you have to study more about object creation in C#. When creating an object of a child class inside the child class constructor, it's calling the base class constructor.

using System;
                    
public class Program
{
    public static void Main()
    {
        Child child = new Child();
    }
}

public class Parent{
    
    public Parent(){
        Console.WriteLine("I am parent");
    }
    
}

public class Child : Parent {
    public Child(){
        Console.WriteLine("I am child");
    }
}

Output

I am parent
I am child

When you creating a class there is a default constructor (the constructor which takes 0 arguments). But when you create another constructor (which takes more than 0 arguments) default constructor will replaced by that. You have to manually create default constructor. So at the runtime, it's trying to call the default constructor of the base class constructor. But since it's gone this error occurs.

  • 'Chaharpa' does not contain a constructor that takes 0 arguments

Then you have to study about nested classes in C#.

You can't just call inner class by it's name. You have to reference it from the outer class.

using System;
                    
public class Program
{
    public static void Main()
    {
        Outer.Inner child = new Outer.Inner();
    }
}

public class Outer{
    
    public Outer(){
        Console.WriteLine("I am outer");
    }
    
    public class Inner : Outer {
        public Inner(){
            Console.WriteLine("I am inner");
        }
    }
}

Output

I am outer
I am inner

That's why you get the error The type or namespace name 'horse' could not be found (are you missing a using directive or an assembly reference?)

This code will work.

using System;
public enum Direction
{ Right, Left,Forward};


 class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;
     
     public Chaharpa(){}
     
    public Chaharpa(int a, int b, int x, int y)
    {
    age = a;
    height = b;
    cordinates_x = x;
    cordinates_y = y;
    }
    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }
    public int Age
    {
        get{ return age; }
    }

    public int Height
    {
        get{ return height;}
    }

    public int Cordinates_x
    {
        get{ return cordinates_x;}
        set{ cordinates_x = value;}
    }
    public int Cordinates_y
    {
        get{return cordinates_y;}
        set{ if (value > 0)cordinates_y = value;}
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }
     
     public class horse : Chaharpa
    {
        public bool is_wild;
       
        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;
        }
        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
        
    }
}


public class Program
{
    public static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;
         
        for (int i = 0; i < 3; i++)
        {
            
            Console.WriteLine("Horse #" + (i + 1));
    
            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());
        
                minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }
        Console.WriteLine();

    Chaharpa.horse ob = new Chaharpa.horse();

    ob.Jump(minAge);
   
    ob.Print();

    }
}

Use one file for one class is a good practice.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Buddhika Chathuranga
  • 1,334
  • 2
  • 13
  • 22