-1

so, I keep getting an use of unassigned local variable 'table' error when debugging my code. I'm trying to make an multiplication table.

Here is the look of my code;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Multiplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDisplayTable_Click(object sender, EventArgs e)
        {
            int table;
            int result;

            for (int i = 1; i < 13; i++) {
                result = table * i;
                lstTable.Items.Add(result);
            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LstTable_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtTable_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Any help and explanation would be appreciated.

2 Answers2

1

Initialize the table variable

int table = 0;
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
0

You never assigned a value to your table variable. Afterward you do table * i but it can't compute because table doesn't have a value

Shogunivar
  • 1,237
  • 11
  • 18