简体   繁体   中英

C# program won't run past object instance

I'm creating a program that should create a List with the overtones of a musical note and display them at the console. The program runs without compiler errors, but the output isn't the expected and I can't seem to understand why. I'll add other features later, but first I want to make this work. The main method looks like this:

    public static void Main (string[] args)
{
    //test method before object instance
    Console.WriteLine ("Test 1");

    //creates object instance with frequency = 110f
    CompositeWave myWave = new CompositeWave (110f);

    //test method after object instance
    Console.WriteLine ("Test 2");

    //CompositeWave method that adds items to overtoneWavesList
    myWave.createNaturalOvertones();

    //prints all overtone frequencies in order
    foreach (KeyValuePair<byte,SineWave> valuePair in myWave.overtoneWavesList) {

        Console.WriteLine (valuePair.Key + " - " + valuePair.Value.tonicFrequecy);
    }

}

the expected output should be something like

        /*
     * Test 1
     * Test 2
     * 0 - 220
     * 1 - 330
     * 2 - 440
     * 3 - 550
     * ... so on
     * /

but instead the output is just

        /*
     * Test 1
     * /

and the program never finalizes and never goes any further.

These are the classes I'm using in the namespace (they still have unimplemented features, like the amplitude of the waves):

Basic class for sound waves (the frequency can't be higher than 24K because it then becomes inaudible):

    /// <summary>
/// Basic sound wave class.
/// </summary>
public abstract class Wave
{
    //simple wave properties
    public float tonicFrequecy 
    {
        get 
        {
            return tonicFrequecy;
        }
        set
        {
            if (value > 24000f) {
                tonicFrequecy = 24000f;
            } else {
                tonicFrequecy = value;
            }
        }
    }
    public float tonicAmplitude { get; set;}
}

Sine wave class derived from the base wave class:

    /// <summary>
/// Simple sine wave with frequency spectrum concentred in only one fundamental frequency.
/// </summary>
public class SineWave : Wave
{
    //initializer
    public SineWave (float frequency = 440f, float amplitude = 0f)
    {
        this.tonicFrequecy = frequency;
        this.tonicAmplitude = amplitude;
    }
}

And a composite wave class (that I'm instantiating at the Main method) derived from the base wave class ass well:

    /// <summary>
/// Complex sound wave composed of multiple sine waves with different frequencies and amplitudes.
/// </summary>
public class CompositeWave : Wave
{
    //initializer
    public CompositeWave (float frequency = 440f, float amplitude = 0f)
    {
        this.tonicFrequecy = frequency;
        this.tonicAmplitude = amplitude;

        this.overtoneWavesList = new SortedList<byte, SineWave>(1);
    }

    //overtone list that compose the composite wave
    public SortedList<byte,SineWave> overtoneWavesList { get; set;}

    /// <summary>
    /// Creates possible natural overtones and sets all amplitudes to zero.
    /// </summary>
    public void createNaturalOvertones ()
    {
        float overtoneFrequency = 0f;

        for (byte i = 2; overtoneFrequency <= 24000f; i++) {

            //sets overtone frequency as multiple of fundamental
            overtoneFrequency = this.tonicFrequecy * (float)i;
            //sets list key and add overtone to list
            int key = (int)i - 2;
            this.overtoneWavesList.Add ((byte)key, new SineWave(overtoneFrequency));
        }
    }
}

I've tried pretty much everything and can't understand why it doesn't go any further than Test 1 :( I know this is a very long post to a simple question, I'm sorry about that, but a big thanks in advance to any response!

You have to code like below for the Wave class.

public abstract class Wave
{
    private float _tonicFrequency;

    //simple wave properties
    public float tonicFrequecy
    {
        get { return _tonicFrequency; }
        set
        {
            if (value > 24000f)
            {
                _tonicFrequency = 24000f;
            }
            else
            {
                _tonicFrequency = value;
            }
        }
    }

    public float tonicAmplitude { get; set; }
}

You are probably getting a StackOverflow exception during the construction of your CompositeWave instance because your tonicFrequency property is self-referencing:

public float tonicFrequecy 
{
    get 
    {
        return tonicFrequecy;  // this points back to itself
    }

A property is an abstraction over class data - it does not provide the actual storage for class data. You need a separate field in the class for storage. For example:

private float _tonicFrequency; // this private field stores the data

// the property uses the private field for storage
public float tonicFrequency {
  get { return _tonicFrequency; }
  set { if (value > 24000f) _tonicFrequency = 24000f; else _tonicFrequency = value; }
}

In the case of auto-properties (like your tonicAmplitude ), you don't have this problem because the compiler automatically adds an internal field for you -- but there certainly is a separate private field backing that property.

tonicFrequency的设置实现是为自己设置一个值,因此程序进入无限循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM