当我尝试访问频率和持续时间属性时,它说:“在当前上下文中不存在。”
这是我的一些代码:
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
}
}
}
}
这是另一页代码的另一部分:
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
}
我是c#的新手,并且已经尝试了几个小时,所以请记住这一点。
答案 0 :(得分:2)
您需要在Frequency
内调用Duration
和foreach
字段:
foreach(Note MyNote23 in notes)
{
Console.Beep(MyNote23.Frequency, MyNote23.Duration);
}
但是我建议您使用Properties而不是公共字段。
参考文献: Why Properties Matter
答案 1 :(得分:0)
您正在Song类中的Note类中调用变量initialize,因此您需要先初始化Note类,然后调用其变量
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;
}
}