Click here to Skip to main content
14,486,572 members
Rate this:
Please Sign up or sign in to vote.
See more:
Maybe I'm overextending the capabilities of the language. I'm working on a program to train myself on keyboards of the musical variety using MIDI. I've created two controls, one representing the keyboard and the other representing the musical treble and bass staffs. Between the form, keyboard and staff I've developed a number of conversion tables, usually one, but sometimes two, per control. Some of my code requires that one control access a table from the other. This quickly became too tangled for the clarity of the code or the dependency of one for the other.

So I thought I'd just accumulate the tables into one class and make it static so I don't need to declare an instance of it. I started getting errors in the code and as I corrected one thing a new problem crept up. I've tried different arrangements, including creating public properties to access array elements. And I'll probably have some Dictionaries eventually.

The most notable error I get is when using either a foreach() or for(;;) loop the error I get is that an instance of the class is required.

Questions: How should I declare the class or variables within it? Or, how should I be accessing the data? Or is a static class not the way to go?

What I have tried:

Note that I've also tried to declare the array without the constructor.

namespace Keyboard_Tutor
{
	class MTables {

		private static Note[] _notes;

		public Note this[int i] {
			get => _notes[i];
		}

		static MTables() {
			_notes = new Note[] {
				new Note(1, 21, 0),
				new Note(2, 22, -1),
				new Note(3, 23, 1),
				new Note(4, 24, 2),
				new Note(5, 25, -1),
				new Note(6, 26, 3),
				new Note(7, 27, -1),
				new Note(8, 28, 4),
				new Note(9, 29, 5),
				new Note(10, 30, -1),
				new Note(11, 31, 6),
				new Note(12, 32, -1),
Posted
Updated 31-Jan-20 20:35pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

If the data is static in nature, using a static list or array makes perfect sense. If you're getting errors when looping the list, we should see the code which generates the error.

In order to loop the notes, you probably would like to make the array or collection public in order to access it from outside this class.

Something like
class MTables {

   public static Note[] Notes;

   static MTables() {
      Notes = new Note[] {
         new Note(1, 21, 0),
         new Note(2, 22, -1),
         new Note(3, 23, 1),
      };
   }
}

And using
foreach (Note n in MTables.Notes) {
   // do something
}
   
v3
Comments
Bertha Wwallace 1-Feb-20 2:44am
   
Hi Wendelius, in your answer, the indexer this[] is not needed at all any more, you are not using it. The outside class is accessing the Notes array directly without the indexer... which might not be desired. Of course, you can always encapsulate a private field into an accessible property of some useful collection type.
Wendelius 1-Feb-20 3:24am
   
Good point, I forgot to remove that when copying the original code. Now removed
Rate this:
Please Sign up or sign in to vote.

Solution 2

"The most notable error I get is when using either a foreach() or for(;;) loop the error I get is that an instance of the class is required."
-- that's clear because your indexer is not (and cannot be) static. The 'this' in
public Note this[int i] is the instance that's required.

If you intend the class MTables to be static, then define it so:"static class MTables".
The array you want to access needs to be a static method, e.g.
public static Note GetNote(int i) { return _notes[i]; } 
Then you'd use it like so:
for(int i = 0; i < maxNote; i++)
{
    var note = MTables.GetNote(i); ...
}

If OTOH you want to use an indexer this[] by all means, then don't make the class static, but then you have to create an instance when you want to index into it:
var table = new MTables();
for(int i = 0; i < maxNote; i++)
{
    var h = table[i];
}
You cannot use foreach unless your MTables class implements the IEnumerable<Note> interface, for example like here.
   
Comments
Lilith.Cal 1-Feb-20 16:44pm
   
Thanks to all. I'd actually resolved the issue before your responses but I couldn't find the post to try to delete it. Essentially I found I needed to make pretty much everything static. I'm still bummed about the indexing, but...

I also had trouble with some of my error codes which would occur at run time. Turns out I was compiling in Release mode (to test a previous problem) and couldn't find where the code was when in breakpoint mode.

Once again, thanks for your assistance.

Lil

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100