2

So I am so fresh into the world of programming, starting new, I decided to start messing around in C# to create simple apps from ideas that come to mind, with this little app, I'm trying to have multiple TextBoxes named d1,d2,d3,d4,etc... the user inserts numbers into the textboxes then clicks button1, which begins the process in the code below creating a new list which contains all of the values of the textboxes and then the list is converted to an array and the array is then converted into an int array, etc....

BUT, when starting the application and I add values to the textboxes and then click button1, it shows 2 error like shows in the //gray code line below

Please help.

private void button1_Click(object sender, EventArgs e)
{
    List<string> dodo = new List<string>();
    dodo.Add(d1.Text); dodo.Add(d2.Text); dodo.Add(d3.Text); dodo.Add(d4.Text); dodo.Add(d5.Text);
    dodo.Add(d6.Text); dodo.Add(d7.Text); dodo.Add(d8.Text); dodo.Add(d9.Text); dodo.Add(d10.Text);
    dodo.Add(d11.Text); dodo.Add(d12.Text); dodo.Add(d13.Text); dodo.Add(d14.Text); dodo.Add(d15.Text);
    dodo.Add(d16.Text); dodo.Add(d17.Text); dodo.Add(d18.Text); dodo.Add(d19.Text); dodo.Add(d20.Text);
    foreach(string numb in dodo)
    {
        if (numb == "")
            numb = "0"; //numb word has a red underline
    }

    string[] terms = dodo.ToArray();
    int[] valv = {};
    int x = 0;
    for(int i=0;i<=19;i++)
    {
        valv[i] = int.Parse(terms[i]); //the ; in the end has a red underline and shows "FormatException was unhandled" error
        i++;
        x = x + valv[i];
    }
     string myString;
    myString = x.ToString();
    Result1.Text = myString;
}
5
  • First the underlining are called squiggly and secondly if you put you mouse over it tell you what the problem is. Commented Apr 2, 2018 at 11:17
  • @Franck True, it tells me "Input String was not in a correct format" in the valv[i] = int.Parse(terms[i]); line in the code.. what can be done to fix it? Commented Apr 2, 2018 at 11:24
  • That is something you have to learn. But i can give you a big hint. Put a breakpoint at your dodo.ToArray() and check what is the value of each of the items in the array. Commented Apr 2, 2018 at 11:49
  • Within your for loop, you are increasing i. As you already do that inside the for statement itself, you are skipping values. Commented Apr 2, 2018 at 12:56
  • When you initialize an array, you have to give it a size. You cannot just add values at random indices (your valv array) Commented Apr 2, 2018 at 12:59

2 Answers 2

1

you can't change the iteration variable which is numb in your case. Please change in the List container instead

        List<string> dodo = new List<string>();
        dodo.Add(d1.Text); dodo.Add(d2.Text); dodo.Add(d3.Text); dodo.Add(d4.Text); dodo.Add(d5.Text);
        dodo.Add(d6.Text); dodo.Add(d7.Text); dodo.Add(d8.Text); dodo.Add(d9.Text); dodo.Add(d10.Text);
        dodo.Add(d11.Text); dodo.Add(d12.Text); dodo.Add(d13.Text); dodo.Add(d14.Text); dodo.Add(d15.Text);
        dodo.Add(d16.Text); dodo.Add(d17.Text); dodo.Add(d18.Text); dodo.Add(d19.Text); dodo.Add(d20.Text);

        int k = 0;

        foreach (string numb in dodo)
        {
            if (numb == "")
            {
                //numb = "0"; //numb word has a red underline
                dodo[k] = "0";
            }

            k++;
        }

Now your code on parsing into integer won't give any runtime error.

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

Comments

1

The first line "tells" you that you are not able to assign a new value to the variable which is used as a foreach iteration variable.

The second line, "tells" you that you have string value which is not able to be parsed correctly (e.g. user put string which is not a number). To avoid this you can use Int32.TryParse method instead, which will safely try to parse the given string.

The best and easiest way to achieve what you need is using LINQ methods, here is the example based on few things/assumptions:

  1. Since you are converting empty strings into zeros, you could simply skip those entries from counting
  2. To avoid FormatException, you should use TryParse method instead. Since TryParse method will safely parse the given string, you don't even have to filter empty strings at all (they will be skipped). However, I deliberately left filtering part, to get you a better overview of a solution.
  3. You can use list initializer to make list initialization more readable

Solution:

List<string> dodo = new List<string>()
{
   d1.Text, d2.Text //...others
};

int sum = dodo
        .Where(item => !String.IsNullOrEmpty(item))
        .Sum(item =>
        {
            if (Int32.TryParse(item, out int parsedItem))
            {
                return parsedItem;
            }
            return 0;
        });

You can get more familiar with LINQ and used methods on following link

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.