Skip to content

Instantly share code, notes, and snippets.

@rstackhouse
Created August 8, 2017 02:08
Show Gist options
  • Save rstackhouse/ff10ded499cfc7d5608a81d9f49f5a7a to your computer and use it in GitHub Desktop.
Save rstackhouse/ff10ded499cfc7d5608a81d9f49f5a7a to your computer and use it in GitHub Desktop.
Message Box Using Generic Stack
public class MessageBox
{
private Stack<string> messages = new Stack<string>();
public int MessageCount { get { return messages.Count; } }
public void AddMessage(string message)
{
messages.Push(message);
}
public int FindMessage(string message)
{
for (var i = 0; i < messages.Count(); i++)
{
if (messages.ElementAt(i) == message)
{
return i;
}
}
return -1;
}
public void RemoveMessage(string message)
{
var temp = new Stack<string>();
var i = FindMessage(message);
if (i > -1)
{
for (var j = (messages.Count - 1); j >= 0; j--)
{
if (j != i )
{
var msg = messages.ElementAt(j);
temp.Push(msg);
}
}
messages.Clear();
messages = temp;
}
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
foreach(var s in messages)
{
sb.Append(s);
sb.Append('\n');
}
return sb.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment