0

I have the whole code working but need a little help getting the converted array to display correctly in the wordListBox. Any help would be appreciated. The code I have is listed below and the string[] word is declared in the class level.

    private void Convert(string[] wordArray, int count)
    {
        //Convert numbers
        for (int index = 0; index < count; index++)
        {
            if (numbers[index] == 1)
            {
                wordArray[index] = "one";
            }
            else if (numbers[index] == 2)
            {
                wordArray[index] = "two";
            }
            else if (numbers[index] == 3)
            {
                wordArray[index] = "three";
            }
            else if (numbers[index] == 4)
            {
                wordArray[index] = "four";
            }
            else if (numbers[index] == 5)
            {
                wordArray[index] = "five";
            }
            else if (numbers[index] < 1)
            {
                wordArray[index] = "lower";
            }
            else
            {
                wordListBox.Items.Add("higher");
            }
        }
    }
    private void ConvertButton_Click(object sender, EventArgs e)
    {
        wordListBox.Items.Clear();
        Convert(word, count);
        wordListBox.Items.Add(word);
    }

2 Answers 2

1

I would use List<string> instead of string[] for the word variable and method parameter wordArray, so you don't have to initialize the array size. In the ConvertButton_Click you are missing a foreach loop that iterates trough all of the elements in wordArray and adds them to the wordListBox. Here is an example:

int[] numbers = { -5, 3, 6, 9, -2, 1, 0, 4};

int count = 8;

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

private void Convert(List<string> wordArray, int count)
{

    //Convert numbers
    for (int index = 0; index < count; index++)
    {
        if (numbers[index] == 1)
        {
            wordArray.Add("one");
        }
        else if (numbers[index] == 2)
        {
            wordArray.Add("two");
        }
        else if (numbers[index] == 3)
        {
            wordArray.Add("three");
        }
        else if (numbers[index] == 4)
        {
            wordArray.Add("four");
        }
        else if (numbers[index] == 5)
        {
            wordArray.Add("five");
        }
        else if (numbers[index] < 1)
        {
            wordArray.Add("lower");
        }
        else
        {
            wordArray.Add("higher");
        }
    }
}
private void ConvertButton_Click(object sender, EventArgs e)
{
    wordListBox.Items.Clear();
    Convert(word, count);
    foreach (var item in word)
    {
        wordListBox.Items.Add(item);
    }
}

Result:

result

Sign up to request clarification or add additional context in comments.

Comments

0

Try:

private void ConvertButton_Click(object sender, EventArgs e)
    {
        wordListBox.Items.Clear();
        Convert(word, count);
        wordListBox.Items.AddRange(word);
    }

3 Comments

I tried that, it threw this message to me. System.ArgumentNullException: 'Value cannot be null. Parameter name: item
Is you array named word or WordArray? 2 ways in your code.
My array is this public partial class Form1 : Form { //Class Variables const int SIZE = 12; int[] numbers = new int[SIZE]; string[] word = new string[SIZE]; int count;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.