473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Method name expected error

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. namespace FindFactors
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             ThreadWork threadwork = new ThreadWork();
  16.  
  17.             Console.Write("Enter a number to find the factors of: ");
  18.             string max = Convert.ToString(Console.Read());
  19.  
  20.             Thread countOdd = new Thread(new ThreadStart(threadwork.countOdd(max)));
  21.             Thread countEven = new Thread(new ThreadStart(threadwork.countEven(max)));
  22.         }
  23.     }
  24. }
  25.  
  26.  
And here's ThreadWork.cs

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace FindFactors
  8. {
  9.     class ThreadWork
  10.     {
  11.         public void countOdd(string max)
  12.         {
  13.             long i = 0;
  14.             long n = Convert.ToInt64(max);
  15.             for (i = 1; i <= n; i += 2)
  16.             {
  17.                 if ((n % i) == 0)
  18.                 {
  19.                     Console.WriteLine(i);
  20.                 }
  21.             }
  22.         }
  23.  
  24.         public void countEven(string max)
  25.         {
  26.             long j = 0;
  27.             long o = Convert.ToInt64(max);
  28.             for (j = 2; j <= o; j+=2)
  29.                 {
  30.                 if ((o % j) == 0)
  31.                     {
  32.                         Console.WriteLine(j);
  33.                     }
  34.         }
  35.     }
  36.     }
  37. }
  38.  
  39.  
What am I doing wrong here? I keep getting the "Method name expected" error on

Expand|Select|Wrap|Line Numbers
  1. Thread countOdd = new Thread(new ThreadStart(threadwork.countOdd(max)));
  2.             Thread countEven = new Thread(new ThreadStart(threadwork.countEven(max)));
  3.  
but I'm not quite sure why...
Oct 19 '10 #1
1 13319
Curtis Rutland
3,256 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         ThreadWork tw = new ThreadWork(100);
  6.         Thread countOdd = new Thread(new ThreadStart(tw.CountOdd));
  7.         Thread countEven = new Thread(new ThreadStart(tw.CountEven));
  8.         Console.WriteLine("Odd Factors:");
  9.         countOdd.Start();
  10.         countOdd.Join();
  11.         Console.WriteLine("Even Factors:");
  12.         countEven.Start();
  13.         countEven.Join();
  14.         Console.ReadKey();
  15.     }
  16. }
  17.  
  18. class ThreadWork
  19. {
  20.     private int max;
  21.  
  22.     public ThreadWork(int max)
  23.     {
  24.         this.max = max;
  25.     }
  26.  
  27.     public void CountOdd()
  28.     {
  29.         long i = 0;
  30.         long n = Convert.ToInt64(max);
  31.         for (i = 1; i <= n; i += 2)
  32.         {
  33.             if ((n % i) == 0)
  34.             {
  35.                 Console.WriteLine(i);
  36.             }
  37.         }
  38.     }
  39.  
  40.     public void CountEven()
  41.     {
  42.         long j = 0;
  43.         long o = Convert.ToInt64(max);
  44.         for (j = 2; j <= o; j += 2)
  45.         {
  46.             if ((o % j) == 0)
  47.             {
  48.                 Console.WriteLine(j);
  49.             }
  50.         }
  51.     }
  52. }
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.
Oct 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Andrew Edwards | last post by:
Could someone please tell me what I'm doing wrong? The compiler barks at me whenever I throw (bad_alloc). If I remove this the code compiles just fine. template < class DT, class KF > class...
6
by: Zenon | last post by:
Folks, I cannot figure out why I am getting an error: Error E2303 EngineX.hpp 19: Type name expected. Here is my code. Can you please help? #ifndef EngineX__hpp #define EngineX__hpp ...
1
by: TAM | last post by:
Hi, I have a simple JavaScript code that ensures that all the form fields are filled and there is also a function that checks if the email is a valid address. For some reason IE is giving...
3
by: Jeff Johnson | last post by:
I'm getting a Method Name Expected error when I try to compile. The error happens here: imgImage.Click += new ImageClickEventHandler(updateColours("X")); Can someone tell me what I'm doing...
4
by: Vishu | last post by:
I m getting this error "Mehtod name expected" in my following code. protected int GetSelectedIndex(string CID) { DataTable dt = ddlDataSet.Tables; for(int iLoop = 0; iLoop <= dt.Rows.Count - 1;...
4
by: Kiyomi | last post by:
Hello, I am trying to replace my alert message box with a popup page. In my page behind,
1
by: StevePBurgess | last post by:
I am using a script (see below) to check if cookies are enabled on my website. The script seems to work but I get an Object Expected error. The error repor says it is at the line incidated by the...
1
by: finizaini | last post by:
I'm receiving an "Object Expected" Error (Line:309, Char:0). I'm confused as to what is happening.Also, I can't run this code using other browser such as Fire Fox. Thispage only can view using IE....
3
by: =?Utf-8?B?YW5kcmV3?= | last post by:
I have a web application demo page and a web service. On my machine everything works great. In our test environment the web service is working fine... when I point the demo page on my machine...
2
by: tridirk | last post by:
Hi; I am getting a Objceted Expected Error on my forum site. I can't find what is wrong? Line: Char: Error: Object expected Code:0 the site is My SMF Forum
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.