Zi0n1
Zi0n1

Reputation: 594

Issue with object reference being required for the non-static field

I have the below code and my issue is that when I reference the variables it gives me the error: an object reference is required for the non-static field, method, or property for each variable. I know it is something to do with my public int and public double being non static but I am not sure how to fix it. Could someone show me possibly?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework3
{
    public class Program
    {

           public int number_of_scores;
           public double score, total_score = 0, high_score, low_score, average;


    } 
    class Class1{

        static void Main(string[] args){


            Console.Write("please enter the number of scores that you
wish to process? ");
            Program.number_of_scores = int.Parse(Console.ReadLine());

            Console.Write("Please enter score " + 1 + " ");
            Program.score = double.Parse(Console.ReadLine());

            Program.high_score = Program.score;
            Program.low_score = Program.score;

            Program.total_score = Program.total_score = Program.score;


            for (int i = 2; i <= number_of_scores; i++);
         }  
 }

     class class2
        {
         static void Main(string[] args){


                Console.Write("Please enter score " + i + " ");
                Program.score = double.Parse(Console.ReadLine());

                Program.total_score = Program.total_score + Program.score;

                if(Program.score > Program.high_score)
                Program.high_score = Program.score;

                if(Program.score < Program.low_score)
                Program.low_score = Program.score;
        }
            }

      class Class3
     {
          static void Main(string[] args){

          Program.average = Program.total_score / Program.number_of_scores;

          Console.WriteLine("you entered " + Program.number_of_scores +
 " scores");
          Console.WriteLine("The high score is " + Program.high_score);
          Console.WriteLine("The low score is " + Program.low_score);
          Console.WriteLine("the average score is " + Program.average);
     }
      }

Upvotes: 1

Views: 80

Answers (2)

sparky68967
sparky68967

Reputation: 647

You need to declare number_of_scores and score (and the other variables) as static.

public static int number_of_scores;
public static double score, //etc

Upvotes: 1

Eric J.
Eric J.

Reputation: 150238

In the line:

Program.number_of_scores = int.Parse(Console.ReadLine());

you try to reference the instance variable number_of_scores from a static method.

The most trivial way to get that to work is to declare number_of_scores as static:

static public int number_of_scores;

Some of your other fields have the same issue.

Upvotes: 1

Related Questions