Samurai
Samurai

Reputation: 189

C# Can't access properties from class

I have a some of the instructions from my course: enter image description here

And When I try to acess the frequency and duration properties it says: "doesn't exist in current context."

Here is a some of my code:

namespace SongPlayer
{

// This class is provided as part of the Activity Starter program.
class Note
{
    // Define common note frequencies
    public static int NOTE_C = 524;
    public static int NOTE_D = 594;
    public static int NOTE_E = 660;
    public static int NOTE_F = 698;
    public static int NOTE_G = 784;
    public static int NOTE_A = 880;
    public static int NOTE_B = 988;

    // Define common note durations (in milliseconds)
    public static int DURATION_WHOLE = 1600;
    public static int DURATION_HALF = 800;
    public static int DURATION_QUARTER = 400;    

    // Declare the properties for this note
     public int Frequency;
     public int Duration;

    // This constructor requires the Frequency and Duration for this note
    public Note(int frequency, int duration)
    {
        this.Frequency = frequency;
        this.Duration = duration;

    }

}

// This class code is ADDED FOR ACTIVITY by the student
class Song
{
    // student code to implement the Song class goes here




        public string Name; //HERE
        public LinkedList<Note> notes; //HERE222
        public Song(string name) //This is a constructor method
        {
            this.Name = name; // "this.Name is referring to THERE while " = name " is reffering to the paramenter.
            this.notes = new LinkedList<Note>(); //This is refferring to THERE222
        }


        public void AddNote(int frequency, int duration)
          {
             Note MyNote = new Note(frequency, duration); //Class instance.
             notes.AddLast(MyNote);

          }


    /// <summary>
    /// 
    /// </summary>
    public void Play()
    {
        foreach(Note MyNote23 in notes)
        {
          //I need to access the properties here
        }


    }


}

}

Here is another part of my code from another page:

private void initializeSongs()
    {
        songs = new LinkedList<Song>(); 
        //Song Mysong = new Song("Marry had a little lamb"); //I barly even know what i'm bloody doing. //inputting name
        Song song1 = new Song("Marry had a little lamb"); //retriver name.
        song1.AddNote(Note.NOTE_B, Note.DURATION_QUARTER); //I think I have to do this for each note. Bloody damit.
        song1.AddNote(Note.NOTE_A, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_G, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_A, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_B, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_B, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_B, Note.DURATION_HALF);
        song1.AddNote(Note.NOTE_A, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_A, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_A, Note.DURATION_HALF);
        song1.AddNote(Note.NOTE_B, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_D, Note.DURATION_QUARTER);
        song1.AddNote(Note.NOTE_D, Note.DURATION_HALF);


        songs.AddLast(song1); 
        SongListBox.Items.Add(song1.Name);   // add song name to list box


    }

I am new to c# and I have been trying for hours so please keep that in mind.

Upvotes: 2

Views: 1877

Answers (2)

TAHA SULTAN TEMURI
TAHA SULTAN TEMURI

Reputation: 5151

You are calling variable initialize in Note class inside Song class so you need to initialize Note class first then call its variable

public void Play()
{  
    Note note = new Note();

    foreach(Note MyNote23 in notes)
    {
        // I need to access the properties here
        int f = note.Frequency;
        int d = note.Duration;         
    }
}

Upvotes: 0

SᴇM
SᴇM

Reputation: 7213

You need to call your Frequency and Duration fields inside foreach:

foreach(Note MyNote23 in notes) 
{ 
    Console.Beep(MyNote23.Frequency, MyNote23.Duration); 
} 

But I would suggest you to use Properties instead of public fields.

References: Why Properties Matter

Upvotes: 2

Related Questions