0

I get the following error when compiling my application:

An object refference is requied for non-static field, method or property

This is the code:

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

namespace ConsoleOpener
{
    class Program
    {
        public static int casse;
        Random rnd = new Random();
        public static int openIt(int casse)
        {
           Int32 skin = 0;
           if (casse==1)
           {
               skin = rnd.Next(1, 3);
           }
            return skin;
        }


        public static void Main(string[] args)
        {
            Console.WriteLine("Choose one of cases:");
            Console.WriteLine("1. TEST CASE");
            int casse = Console.Read();
            openIt(casse);
        }
    }
}

How can I solve this? EDIT-- Please... i wouldn't write this if i find an answer. Everyone says (set something as static BUT ALMOST EVERYTHING IS NOW STATIC

Mudzay
  • 71
  • 8

1 Answers1

0

You can solve this problem by declaring your Random rnd; parameter as static:

static Random rnd = new Random();   // declaring rnd as static
public static int openIt(int casse)
{
   Int32 skin = 0;
   if (casse==1)
   {
      skin = rnd.Next(1, 3);
   }
   return skin;
}
croxy
  • 4,082
  • 9
  • 28
  • 46
  • Yes, thank you. It worked but theres 1 more SAME error but with ConsoleOpener.Program.openIt(int) – Mudzay Jan 27 '16 at 10:45