Click here to Skip to main content
15,850,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make a multithreaded Fibonnaci series program. I keep getting errors on the nthNumber part of the code. I'm not sure how to fix it, I have tried a few different things and nothing has really gotten rid of the error.
C#
{

    public class Worker
    {
        private readonly int nthNumber;

        public Worker(int nthNumber) => this.nthNumber = nthNumber;

        public void Calculate(object o)
        {
            Console.WriteLine("Fibonacci {0} is {1}", nthNumber, Fibonacci(nthNumber));
        }

        internal static int Fibonacci(int n)
        {
            if (n == 0)
            {
                return 0;
            }
            else if (n == 1)
            {
                return 1;
            }
            else
            {
                return Fibonacci(n - 1) + Fibonacci(n - 2);
            }
        }



        internal static readonly EventWaitHandle AllWorkersCompleted = new EventWaitHandle(false, EventResetMode.AutoReset);
        private static int numberOfWorkers = 0;
        private readonly int nthNumber;

        public Worker()
        {
            Interlocked.Increment(ref numberOfWorkers);
            this.nthNumber = nthNumber;
        }

        public void Calculate(object o)
        {
            Console.WriteLine("Fibonacci {0} is {1}", nthNumber, Fibonacci(nthNumber));
            if (Interlocked.Decrement(ref numberOfWorkers) == 0)
            {
                AllWorkersCompleted.Set();
            }


What I have tried:

I've tried suggestions given by Visual Studio itself and none of the suggestions worked.
Posted
Updated 2-Sep-20 3:22am
v2
Comments
F-ES Sitecore 2-Sep-20 7:58am    
What is the error and what line does it occur on?
Hankatoo1618 2-Sep-20 8:03am    
Any line with nthNumber on it, it shows an error that says the type Worker already contains a definition for nthNumber.
Member 12901943 2-Sep-20 8:14am    
It's not simply that you have nthNumber defined twice "private readonly int nthNumber;" -- first line of the worker class then again after the AllWorkersCompleted defintion?
Hankatoo1618 2-Sep-20 8:32am    
yes

1 solution

Quote:
the type Worker already contains a definition for nthNumber

And indeed it does: one at the top of the file:
C#
public class Worker
{
    private readonly int nthNumber;

    public Worker(int nthNumber) => this.nthNumber = nthNumber;
And one nearer the bottom:
C#
private static int numberOfWorkers = 0;
private readonly int nthNumber;

public Worker()
{
    Interlocked.Increment(ref numberOfWorkers);
    this.nthNumber = nthNumber;
}
And your parameterless constructor is useless, as it assigns the value of nthNumber to itself. Since the method does not define a local or parameter variable called nthNumber it means that this.nthNumber and nthNumber are the same variable.
 
Share this answer
 
Comments
CPallini 2-Sep-20 9:23am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900