Created
August 8, 2017 02:08
-
-
Save rstackhouse/ff10ded499cfc7d5608a81d9f49f5a7a to your computer and use it in GitHub Desktop.
Message Box Using Generic Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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