-4

It is my understanding that a public variable declared at the class level of a program was available everywhere else in the program.

In the following code:

namespace Lab13
{
    class Program
    {
       int lower = int.Parse(Console.ReadLine());


        static void Main(string[] args)
        {
            Console.Write("Enter a lower bound number:");


            lower = int.Parse(Console.ReadLine());
        }
    }
}

I get a compile error with regards to my assignment of lower.

Igor
  • 60,821
  • 10
  • 100
  • 175
Benjamin Boyce
  • 305
  • 1
  • 4
  • 11

1 Answers1

1

It's an instance field, and instance members couldn't be accessed from static members.

Since I see that it's just a sample code, changing int lower to static int lower will be sufficient to access the whole field from Main method.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • 1
    Please try to not answer questions that have been answered hundreds of times on this site. Just flag them as a duplicate. – Dispersia Jan 17 '17 at 21:30
  • 1
    @Dispersia Yeah, I've found this http://stackoverflow.com/questions/32572554/unable-to-use-instance-variable-in-a-static-method. I've voted to re-open this Q&A to be able to mark it as duplicate... – Matías Fidemraizer Jan 17 '17 at 21:31
  • 1
    Thanks for the downvotes, which no one could understand them as my answer addresses OP's problem. – Matías Fidemraizer Jan 17 '17 at 22:10