Go Down

Topic: [C#] Determing next free position in list of integers (Read 1 time) previous topic - next topic


Ok, maybe this is the right forum this time, as apparently C# questions are unwelcome in the "Programming" forum...
This is part of a GUI for interfacing with the Arduino, but the question doesn't deal with communication either.

Basically I have a list of graphs, that are numbered:
"Graph 1"
"Graph 2"
"Graph 3"
"Graph 4"

Adding them is easy when the numbers are sequential. Where it gets tricky is that I also want to remove graphs while preserving the correct order.

So if I remove "Graph 3", I have this:
"Graph 1"
"Graph 2"
"Graph 4"

And then next time I press the Add button, it needs to add another "Graph 3". And then continue with "Graph 5".

I seem to be too stupid to figure this out smiley-sad
Or maybe I'm using the wrong collection. I thought about using some sorting mechanism like IComparable, but that doesn't help with the fact that I first need to figure out the correct number of the graph. Or can I rig a sorting interface, drop a string called "Graph" into the collection and let it figure where to place it?


Basically I have a list of graphs, that are numbered:

Vague, poorly worded questions about non-Arduino issues aren't exactly welcome here, either.

What type is the "list of graphs"?

And then next time I press the Add button, it needs to add another "Graph 3"

Why? Why wouldn't it add "Monkeys are fun5"?

Or maybe I'm using the wrong collection.

Since you haven't told us what collection you are using, I'll agree that it isn't the right type. Or, maybe you're just handling it incorrectly. Or, it's just that time of month.

The art of getting good answers lies in asking good questions.


It's a Chart Control. So I can add Points like this:
chart.Series["Graph 1"].Points.AddXY

I also have the the data for the points in a SortedList, where the key is the string and the value is a class that contains some variables. I then calculate the points from these variables when adding them to the chart.

But the problem can also be approached entirely theoretically without all the .NET stuff. Something like counting up from 1, then comparing that value to all the numbers that have already been assigned and then determine the first free one.

And now that I think about, that's probably the best way. I can probably just do something like Contains() with the keys! This is probably a case of rubber duck debugging:


Wouldn't it be easier to store the names (I presume "Graph2" etc is a name) in an array so that "Graph2" is in graphArray[2] and set graphArray[2] = "blank" when "Graph2" is deleted.

You can then search the array for the first instance of "blank" and you will know from the array index what number to suffix to the name.

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up