Method name expected error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield

    #1

    Method name expected error

    Code:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace FindFactors
    {
        class Program
        {
            static void Main(string[] args)
            {
                ThreadWork threadwork = new ThreadWork();
    
                Console.Write("Enter a number to find the factors of: ");
                string max = Convert.ToString(Console.Read());
    
                Thread countOdd = new Thread(new ThreadStart(threadwork.countOdd(max)));
                Thread countEven = new Thread(new ThreadStart(threadwork.countEven(max)));
            }
        }
    }
    And here's ThreadWork.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace FindFactors
    {
        class ThreadWork
        {
            public void countOdd(string max)
            {
                long i = 0;
                long n = Convert.ToInt64(max);
                for (i = 1; i <= n; i += 2)
                {
                    if ((n % i) == 0)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
    
            public void countEven(string max)
    	    {
    		    long j = 0;
    		    long o = Convert.ToInt64(max);
    		    for (j = 2; j <= o; j+=2)
                	{
                    if ((o % j) == 0)
                        {
                            Console.WriteLine(j);
                        }
            }
    	}
        }
    }
    What am I doing wrong here? I keep getting the "Method name expected" error on

    Code:
    Thread countOdd = new Thread(new ThreadStart(threadwork.countOdd(max)));
                Thread countEven = new Thread(new ThreadStart(threadwork.countEven(max)));
    but I'm not quite sure why...
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    You're not providing the method name, you're trying to call the method.

    You can't call a method with a parameter using a thread, apparently. You could do something like this:

    Code:
    class Program
    {
        static void Main()
        {
            ThreadWork tw = new ThreadWork(100);
            Thread countOdd = new Thread(new ThreadStart(tw.CountOdd));
            Thread countEven = new Thread(new ThreadStart(tw.CountEven));
            Console.WriteLine("Odd Factors:");
            countOdd.Start();
            countOdd.Join();
            Console.WriteLine("Even Factors:");
            countEven.Start();
            countEven.Join();
            Console.ReadKey();
        }
    }
    
    class ThreadWork
    {
        private int max;
    
        public ThreadWork(int max)
        {
            this.max = max;
        }
    
        public void CountOdd()
        {
            long i = 0;
            long n = Convert.ToInt64(max);
            for (i = 1; i <= n; i += 2)
            {
                if ((n % i) == 0)
                {
                    Console.WriteLine(i);
                }
            }
        }
    
        public void CountEven()
        {
            long j = 0;
            long o = Convert.ToInt64(max);
            for (j = 2; j <= o; j += 2)
            {
                if ((o % j) == 0)
                {
                    Console.WriteLine(j);
                }
            }
        }
    }
    Note, the way I did the starts and joins, we might as well have just done it synchronously. But it's just to illustrate a point. It makes no sense to have output like that on a separate thread, because if they are executing at the same time, they'll both be outputting at the same time to the console, and you'll get jumbled results. A better approach would be to store the results in a List<int> or two (maybe OddFactors and EvenFactors) to retrieve after they've been calculated.

    Anyway, notice how I made the ThreadStart objects: with the method names. No parenthesis. That's actually getting a reference to the method as an object, rather than calling the method.

    Comment

    Working...