-5

I'm writing an undo and redo for my notepad. This is my undo and redo code and I have a System.IndexOutOfRangeException exception, how can I fix this? Is there another way to do this?

string[] temp = new string[100];
     int index;
     int currentpostion;

    public Undo()
    {
        index = 0;
        currentpostion = 0;
    }

    public void Set_Text(string s)
    {
        temp[index] = s;
        currentpostion = index;

        ++index;
    }

    public string UndoCons()
    {
        if (currentpostion > 0) 
        { 
           return temp[--currentpostion];
        }
        return null;
    }

    public string RedoCosns()
    {
        if (currentpostion < index)
        {
            return temp[++currentpostion];
        }
        return null;
    }
}

This error occurs when the array is full. What can I do? Can anyone improve this algorithm?

BDL
  • 21,052
  • 22
  • 49
  • 55
  • 1
    if anyone needs more details comment what you want. thanks – Amirmohammad Ramezani Jun 22 '21 at 10:23
  • 2
    Can you show what calls this code, when / how this happens; why `Undo()` resets to `0` etc. In other words, give context to these lines of code so there's a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of it? – Jimi Jun 22 '21 at 10:27
  • 1
    Use `Stack` instead of array or list. You would then Push an item onto the stack, and Pop it back off the stack. – Lasse V. Karlsen Jun 22 '21 at 10:29
  • You need to midofy the method Set_Text() so when number of items in the array is greater than 100 that no more data is added. – jdweng Jun 22 '21 at 10:30
  • 2
    dotnet has a huge variety of collection-classes for all purposes. basically, you _almost_ never want to use an actual `Array`, but either a `List` or something more optimised for your use case. – Franz Gleichmann Jun 22 '21 at 10:31
  • 2
    Does this answer your question? [What is an "index out of range" exception, and how do I fix it?](https://stackoverflow.com/questions/24812679/what-is-an-index-out-of-range-exception-and-how-do-i-fix-it) –  Jun 22 '21 at 10:43
  • 6
    _"how can i fix..Can anyone improve?"_ - I’m voting to close this question because the way it is written will lead to answers that rather than exploring the original cause of the issue, instead go about re-writing (thus we don't learn) the code entirely thus becoming nothing more than a **code review** - which is off-topic for SO. It _may_ be better suited for another SE site but be sure to read the relevant FAQ; and/or re-wording your question as necessary before cross-posting. [ask]. –  Jun 22 '21 at 11:21
  • 1
    @AmirmohammadRamezani please read the notice at the top of your question –  Jun 23 '21 at 04:18

1 Answers1

1

One simple solution would be to use a List instead of an array.

List<string> temp = new List<string>();

It will need some tweaks but it will not limit you to 100 actions.

In any other case, you should decide the strategy. Do you want to delete the initial items? Then remove your first X items and replace them with the latest ones.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61