-1

EDIT: It turns out that the local variable was not the issue. Even after changing it I get the error message from Visual Studio Community (was using VS code before):

"An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module. Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

If I create a new project, and copy and paste the code, a different message appears:

The debug executable 'D:\Desktop\New folder\ConsoleApp1\ConsoleApp1\bin\Debug\netcoreapp3.1\ConsoleApp1.exe' specified in the 'ConsoleApp1' debug profile does not exist

A problem also appears saying the program does not contain a Static Main, but the previous replies say the code works as it is.

OLD:

At college we have done a C# exercise (code will be further down) that for some reason does not work on my computer, despite being exactly the same (I have even copy and pasted someone else's that did work on theirs but not on mine). The code was meant to accept a number from the user and see if it was within a range or not, and depending on that output a message.

For some reason, it appears that a local variable is not being read as it shows up as the wrong color, but if you hover the mouse over the word, a pop-up menu does say it is a local variable. The variable is the 'result' after the 'if' and before the brackets (line 15).

At first we thought it was a problem with Visual Studio Code, so I tried it on Visual Studio Community 2019 and it still did not work. I already have the relevant framework. My teachers think there is a problem with my device, what do you think?

Here is how it looks in VS code

Here is the pop-up menu that appears

Here are the list of problems after debugging

And here is the source code:

using System;

namespace Csharp_learning
{
    class MainDemo
    {
        static int ReadNumber(string prompt, int min, int max)
        {
            int result = 0;
            do 
            {
                Console.Write(prompt);
                string numberString = Console.ReadLine();
                result = int.Parse(numberString);
                if result (result > max || result < min)
                    Console.WriteLine("Please enter a value in the range " + min + " to " + max);
                else
                    break;
            }
            while(true);
            return result;
        }
    }
}

This is my first time using this website, sorry if I did not present this correctly.

user14787797
  • 1
  • 1
  • 3
  • 3
    Chances are that 'result' shouldn't be included before the parentheses on this line: `if result (result > max || result < min)` – ryanwebjackson Dec 08 '20 at 15:42
  • I believe VS Code is treating it as a method when included before the parentheses. Unless you are doing something clever with binary logic, the result variable doesn't make sense in that specific location in the code.. – ryanwebjackson Dec 08 '20 at 15:50
  • In VS 2019, you should have seen a red squiggly line under `result` in the `if` line, telling you that there is something wrong at that point. There will be another red squiggly line after that, but you need to fix the first one in case that is causing the second one. – Andrew Morton Dec 08 '20 at 15:57
  • 1
    After `if` there should *always* be a `(` (maybe with some whitespace inbetween), which is what those error messages are trying to tell you. – Hans Kesting Dec 08 '20 at 16:17

2 Answers2

2
using System;
namespace Csharp_learning
{
  class MainDemo
  {
    public static int ReadNumber(string prompt, int min, int max)
    {
      int result = 0;
      do
      {
        Console.Write(prompt);
        string numberString = Console.ReadLine();
        result = int.Parse(numberString);
        if (result > max || result < min)
                    Console.WriteLine("Please enter a value in the range " + min + " to " + max);
        else
          break;
      }
      while (true);
      return result;
    }
  }
}

You have an extra result before if statement.

As for the other error you can try the link below.

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module

A problem also appears saying the program does not contain a Static Main, but the previous replies say the code works as it is.

Every c# console program has a static main. I was under the impression you already knew that. That part should look a little like below.

using System;

namespace Csharp_learning
{
    class Program
    {
        static void Main(string[] args)
        {
          MainDemo.ReadNumber("Prompt input ", 1, 10);
        }
    }
}

These two different code blocks are most often in different files but you could just try copying in below to see if it works.

    using System;
    namespace Csharp_learning
    {
      class MainDemo
      {
        public static int ReadNumber(string prompt, int min, int max)
        {
          int result = 0;
          do
          {
            Console.Write(prompt);
            string numberString = Console.ReadLine();
            result = int.Parse(numberString);
            if (result > max || result < min)
                        Console.WriteLine("Please enter a value in the range " + min + " to " + max);
            else
              break;
          }
          while (true);
          return result;
        }
      }
    }

namespace Csharp_learning
{
    class Program
    {
        static void Main(string[] args)
        {
          MainDemo.ReadNumber("Prompt input ", 1, 10);
        }
    }
}
Daniel Kelsch
  • 393
  • 2
  • 10
  • Thanks for help, however I still have a problem. I tried it again, this time in Visual Studio 2019. After debugging it says "Build Failed". Then it says the application is in Break Mode and the error "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module. Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." – user14787797 Dec 08 '20 at 17:34
  • Is it just a console app? – Daniel Kelsch Dec 08 '20 at 20:31
  • On Visual Studio Code I used the 'dotnet new console' command in the terminal which I believe is the command to make a console app (I am still new to C#). Do you think I should make a new question about this? – user14787797 Dec 09 '20 at 11:11
  • 'dotnet new console' is the correct command for creating a new c# console program. – Daniel Kelsch Dec 09 '20 at 15:01
0

Your code runs fine for me. It works as expected. The error suggest there is a typo in the project file. Open your CSharp Learning.csproj project file and look for parenthesis that are mismatched or out of place. Another solution would be to create a new project. Then copy your code the the new project.

FrankJames
  • 100
  • 1
  • Are you referring to the code in [Daniel Kelsch's answer](https://stackoverflow.com/a/65202223/1115360)? Because the code shown *in the question* cannot compile with the syntax error. – Andrew Morton Dec 09 '20 at 10:43
  • So I tried copy and pasting into a new project, and it solved most problems except now it says an executable cannot be found and says I need to include a Static Main. I used the new code from Daniel. – user14787797 Dec 09 '20 at 12:37