-1

I am just starting to learn C# and Microsoft Visual Studio. I just created a C# console application project which has a program.cs file. I modified its content like this:

using System;

namespace Calculator
{
    class Calculator
    {
        public static double DoOperation(double num1, double num2, string op)
        {
            double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error

            // Use a switch statement to do the math
            switch (op)
            {
                case "a":
                    result = num1 + num2;
                    break;
                case "s":
                    result = num1 - num2;
                    break;
                case "m":
                    result = num1 * num2;
                    break;
                case "d":
                    // Ask the user to enter a non-zero divisor
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                    }
                    break;
                // Return text for an incorrect option entry
                default:
                    break;
            }
            return result;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
         //   NewClass obj = NewClass(); //this line is causing problem
            bool endApp = false;
            // Display title as the C# console calculator app
            Console.WriteLine("Console Calculator in C#\r");
            Console.WriteLine("------------------------\n");

            while (!endApp)
            {
                // Declare variables and set to empty
                string numInput1 = "";
                string numInput2 = "";
                double result = 0;

                // Ask the user to type the first number
                Console.Write("Type a number, and then press Enter: ");
                numInput1 = Console.ReadLine();

                double cleanNum1 = 0;
                while (!double.TryParse(numInput1, out cleanNum1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput1 = Console.ReadLine();
                }

                // Ask the user to type the second number
                Console.Write("Type another number, and then press Enter: ");
                numInput2 = Console.ReadLine();

                double cleanNum2 = 0;
                while (!double.TryParse(numInput2, out cleanNum2))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput2 = Console.ReadLine();
                }

                // Ask the user to choose an operator
                Console.WriteLine("Choose an operator from the following list:");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\ts - Subtract");
                Console.WriteLine("\tm - Multiply");
                Console.WriteLine("\td - Divide");
                Console.Write("Your option? ");

                string op = Console.ReadLine();

                try
                {
                    result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("This operation will result in a mathematical error.\n");
                    }
                    else Console.WriteLine("Your result: {0:0.##}\n", result);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
                }

                Console.WriteLine("------------------------\n");

                // Wait for the user to respond before closing
                Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "n") endApp = true;

                Console.WriteLine("\n"); // Friendly linespacing
            }
            return;
        }
    }
}

Now, I want to add another C# file in this project and use that class in my program, something like this:

using System;

namespace Calculator
{
    public class NewClass
    {
        public NewClass()
        {
            Console.WriteLine("Hello World");
        }
    }
}

When I am trying to use the NewClass in Program, it is giving an error. I have added it to the same folder in which program.cs resides, still it is not fixed. I got this solution from this link.

So my main questions are:

  1. How to use other classes from the same folder in c# (just like in java, if two programs are in same folder, they are considered to be in same package and can be used without any import statement. Can't something like that be done in this case?)
  2. This is not related to the current topic though, but can we create a project in Microsoft Visual studio which is just a folder containing some C# files?(More specifically, I just want to write C# codes like in a normal IDE,how can I do it without creating any project?)

EDIT: I should have posted the exact error..my bad. So when I am trying to run the program it is showing:

1>------ Build started: Project: ConsoleApp1, Configuration: Debug|AnyCPU ------
C:\Users\<SystemName>\Source\Repos\<UserName>\C-sharp-Practice-Codes\ConsoleApp1\ConsoleApp1\Program.cs(42,28,42,36): error CS1955: Non-invocable member 'NewClass' cannot be used like a method.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Why is NewClass() non-invocable?

Ricky
  • 635
  • 2
  • 5
  • 20
  • 1
    What is the error? Post it as text please, not as screenshot – Sentry Mar 10 '19 at 13:30
  • *it is giving an error*. You bring your car to the mechanic and say "It's not working". Do you think the mechanic is going to be able to fix your car with that information? You need to provide **specific detail** on what isn't working if you want help. – Daniel Mann Mar 10 '19 at 13:32
  • You missed the `new` operator. `NewClass obj = new NewClass();` – Crowcoder Mar 10 '19 at 13:35
  • @Crowcoder … I am really ashamed of my silly mistake.Thank you for pointing out. It is fixed now... :p Can you put some light on the second question? – Ricky Mar 10 '19 at 13:42
  • If you are familiar with Java you can do it similarly. Just make a text file with a `.cs` extension and use the `csc.exe` compiler to build it See: https://stackoverflow.com/questions/553143/compiling-executing-a-c-sharp-source-file-in-command-prompt – Crowcoder Mar 10 '19 at 13:46
  • If you are looking for more help but still low ceremony I can recommend the LINQPad tool. – Crowcoder Mar 10 '19 at 13:49
  • Thank you very much.. :) – Ricky Mar 10 '19 at 13:53

1 Answers1

1

How to use other classes from the same folder in c# (just like in java, if two programs are in same folder, they are considered to be in same package and can be used without any import statement. Can't something like that be done in this case?)

You just missed the new operator. Your line with NewClass should look like this:

NewClass obj = new NewClass();

Read more about new operator in new operator docs page.

This is not related to the current topic though, but can we create a project in Microsoft Visual studio which is just a folder containing some C# files?(More specifically, I just want to write C# codes like in a normal IDE,how can I do it without creating any project?)

You can do that by 2 ways:

  1. by creating blank solution in new project window creator (see more about that);
  2. by opening any directory, like in programming editors – you can easily create new files by solution explorer (see more about that).
Oshawott
  • 539
  • 5
  • 12