user282659
user282659

Reputation: 27

Need Help With a Simple C# Program!

I Want to call a class into the Main method.. And I'm getting this error :s

Code:

using System;


namespace AddMinusDivideMultiply
{
    class Program
    {
        public static int i, j;

        public static void Main()
        {

            Console.Write("Please Enter The First Number  :");
            string temp = Console.ReadLine();
            i = Int32.Parse(temp);

            Console.Write("Please Enter The Second Number :");
            temp = Console.ReadLine();
            j = Int32.Parse(temp);

            Minuz.minus();  // Here its generating an Error - Error    1    The name 'Minuz' does not exist in the current context    


        }
    }

    class Terms
    {
        public static void Add()
        {
            int add;
            add = Program.i + Program.j;
            Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
        }

    class Minuz
    {
        public static void Minus()
        {
        int minus;
        minus = Program.i - Program.j;
        Console.WriteLine("The Subraction Of The First and The Second Number is {0}", minus);
        }
    }
    }
}

Upvotes: 0

Views: 381

Answers (5)

Justin Rusbatch
Justin Rusbatch

Reputation: 4052

Unless its a typo, you're missing a closing bracket for the Terms class. The way its currently written in your post, you would need to put this statement in your Main method:

Terms.Minuz.Minus();

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273169

You have embedded the Minuz class inside the Terms class. If you make it public class Minuz you can call

Terms.Minuz.Minus();

to solve the error. But you probably want to move the Minuz class out of Terms.

Upvotes: 1

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

The problem is that the class Minuz is declared inside the class Terms, and it is private. This means that it is not visible from the Main method.

There are two possible ways to solve it:

  • Make the class Minuz internal or public and chance the call to the Minus method to Terms.Minuz.Minus()
  • Move the declaration of the class Minuz out from the Terms class so that it is instead declared in the namespace.

Also, as pointed out by others; mind the case of the method name. That will be your next problem once the class visibility has been fixed.

Upvotes: 1

Shaihi
Shaihi

Reputation: 3974

That's because the Class Minuz is defined inside the Class Terms so it really is not defined in the context you are trying to use it.

You did not close the definition of Terms before declaring Minuz

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630339

Case matters in C#!

Call this:

Minuz.Minus();

Also, need to change your braces so it's not inside Terms:

class Terms
{
    public static void Add()
    {
        int add;
        add = Program.i + Program.j;
        Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
    }
}

class Minuz
{
    public static void Minus()
    {
    int minus;
    minus = Program.i - Program.j;
    Console.WriteLine("The Subraction Of The First and The Second Number is {0}", minus);
    }
}

Upvotes: 2

Related Questions