DANIEL PEREIRA

DANIEL PEREIRA

  • NA
  • 4
  • 623

Method must have a return type

Dec 25 2017 9:55 AM
Folks, I am an engineer who learned Pascal back in my day and I am trying to get around object-oriented languages, with limited success. I'm just getting started on C#, so please bear with me.
 
I copied some code I found at https://blogs.msdn.microsoft.com/dawate/2009/06/24/intro-to-audio-programming-part-3-synthesizing-simple-wave-audio-using-c/ and now I am trying to run it, but there's something wrong at line 128.
 
The original code was
  1. public enum WaveExampleType  
  2. {  
  3. ExampleSineWave = 0  
  4. }  
  5.   
  6. public class WaveGenerator  
  7. {  
  8. // Header, Format, Data chunks  
  9. WaveHeader header;  
  10. WaveFormatChunk format;  
  11. WaveDataChunk data;  
  12. ///  
  13. }  
  14.   
  15. public WaveGenerator(WaveExampleType type)  
  16. {  
  17. // Init chunks  
  18. header = new WaveHeader();  
  19. format = new WaveFormatChunk();  
  20. data = new WaveDataChunk();  
  21.   
  22. // Fill the data array with sample data  
  23. switch (type)  
  24. {  
  25. case WaveExampleType.ExampleSineWave:  
  26. // Number of samples = sample rate * channels * bytes per sample  
  27. uint numSamples = format.dwSamplesPerSec * format.wChannels;  
  28. // Initialize the 16-bit array  
  29. data.shortArray = new short[numSamples];  
  30. int amplitude = 32760; // Max amplitude for 16-bit audio  
  31. double freq = 440.0f; // Concert A: 440Hz  
  32. // The "angle" used in the function, adjusted for the number of channels and sample rate.  
  33. // This value is like the period of the wave.  
  34. double t = (Math.PI * 2 * freq) / (format.dwSamplesPerSec * format.wChannels);  
  35. for (uint i = 0; i < numSamples - 1; i++)  
  36. // Fill with a simple sine wave at max amplitude  
  37. for (int channel = 0; channel < format.wChannels; channel++)  
  38. {  
  39. data.shortArray[i + channel] = Convert.ToInt16(amplitude * Math.Sin(t * i));  
  40. }  
  41. }  
  42. // Calculate data chunk size in bytes  
  43. data.dwChunkSize = (uint)(data.shortArray.Length * (format.wBitsPerSample / 8));  
  44. break;  
  45. }  
  46. }  
But I get the return type error for line 1. (line 128 at this online editor: http://rextester.com/KOVPK29971). What is wrong here? I get that the intention was to leave room for a future implementation of a different WaveExampleType using the switch/case thing, but why does one have to write anything inside the parenthesis in line 1 and why does it have the same name as the class?
 
I know it's a very simple thing, and I realize the point is I don't understand the whole point behind creating a class and then something else with the same name. Remember, I learned Pascal. Could someone please point how to correct the code and the idea behind it? Thanks in advance!

Answers (2)