-3

Trying to call the procedure cycles() from within my function fibI, but the error is:

An object reference is required for the non-static field, method, or property array_calculator.Fibonacci_panel.cycles()'

Heres the procedure

      public void cycles()
      {
          k++;
      }  

and the function

     public static double fibI(double input, int k)
     {
         if (input == 1 || input == 2)
         {
             return 1;
         }
         else
         {
             double fib1 = 0;
             double fib2 = 1;
             double fibResult = 0;
             for(double i = 1; i < input; i++ )
             {
                 fibResult = fib1 + fib2;
                 fib1 = fib2;
                 fib2 = fibResult;
                 cycles();
             }

             return fibResult; ;
         }
Alex K.
  • 171,639
  • 30
  • 264
  • 288
EPicLURcher
  • 21
  • 1
  • 8
  • Maybe I simply can't see it, but what's the intension for "k"? I can't find any usages besides the "k++" – treze Feb 03 '14 at 15:35
  • possible duplicate of [An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi) (and countless others) – Kirk Woll Feb 03 '14 at 15:39
  • k is used to count the number of times the cycle is ran- its for a school project – EPicLURcher Feb 03 '14 at 15:40

1 Answers1

2

Your "procedure" is not static. To repair you code just change:

public static void cycles() { k++; }

Moreover there isn't a "procedure" in C#. It is a normal function.

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • Note, `k` should be static variable also. But now question - what is passed to `fiBI` – Sergey Berezovskiy Feb 03 '14 at 15:33
  • +1. In all fairness, 'procedure' is a language-agnostic way to refer to any callable sub-program within a larger program. – OnoSendai Feb 03 '14 at 15:33
  • uh it's called a method, not function or procedure – Weyland Yutani Feb 03 '14 at 15:34
  • the function fibI used to work out the nth number in the fibonacci series. Our teacher said to do it like this as well as use recursion which works! We need to implement a counter to count how many cycles are needed to calculate the end value. I plan to call cycles() each cycle to count how many cycles there have been as i cant return it through the fucntion – EPicLURcher Feb 03 '14 at 15:38