Tuesday, 17 September 2013

C#: Being thrown an IndexOutOfRangeException for an unknown reason

C#: Being thrown an IndexOutOfRangeException for an unknown reason

I'm fairly new to C#, and for some reason I'm being thrown an
IndexOutOfRangeException for a substring with the bounds of 0 and 0. I
don't think it's an issue with my scope as I've tested to make sure
everything is defined where it is used.
I'm trying to make a very simple anagram generator:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string[] d = { "Apple", "Bass", "Cat", "Dog", "Ear", "Flamingo",
"Gear", "Hat", "Infidel", "Jackrabbit", "Kangaroo", "Lathargic",
"Monkey", "Nude", "Ozzymandis", "Python", "Queen", "Rat",
"Sarcastic", "Tungston", "Urine", "Virginia", "Wool", "Xylophone",
"Yo-yo", "Zebra", " "};
string var;
int len = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var = textBox2.Text;
//textBox1.Text = d[2];
for (int y = 0; y <= var.Length; y++)
{
for (int x = 0; x <= d.Length; x++)
{
if (d[x].Substring(0, 0).ToUpper() ==
var.Substring(len, len).ToUpper())
{
textBox1.Text = textBox1.Text + "\n" + d[x];
len = len + 1;
}
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}

No comments:

Post a Comment