I am basically just wondering how can I make this while loop repeat continuously forever? I just can’t figure out how to do it. once it reaches the end of the string it throws an exception. I have tried goto but it didn’t work idk if i was just using it in the wrong play or what but i could get it work.
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox2.Checked)
{
int startIndex = 0;
string str = "hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
int length = str.Length;
while (true)
{
Application.DoEvents();
Thread.Sleep(200);
startIndex++;
string str2 = str.Substring(startIndex, 15);
label2.Text = str2;
if (startIndex == length)
{
startIndex = 0;
}
The last valid index in a string (or any array or list or collection) is always the length – 1, not length. Furthermore, since you are taking a substring of length 15, you actually want to stop 15 characters before the end. You’ll want to change your if statement to be