What am I doing wrong here in this C# form?
I was about 98% done with this when I encountered two issues. 1)Now I cannot select from the combo box (could a few minutes ago)and 2) Additional deposit and process button throwing exception toline: string[] fields = Accounts[index];
Exception: System.ArgumentOutOfRangeException: ‘Index was out ofrange. Must be non-negative and less than the size of thecollection.
Parameter name: index’
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;
using System.IO;
namespace Assignment
{
public partial class Assignment : Form
{
public Assignment()
{
InitializeComponent();
}
List<string[]> Accounts = newList<string[]>();
//private int x;
private void Assignment_Load(object sender, EventArgs e)
{
string currentLine;
string[] fields = new string[2];
//Create streamreader
StreamReader AnnuityReader = new StreamReader(“annuities.txt”);
//read each line from the file, split and store in anarray
while (AnnuityReader.EndOfStream == false)
{
currentLine = AnnuityReader.ReadLine(); //
fields = currentLine.Split(‘,’);
Accounts.Add(fields);
cmbAccount.Items.Add(fields[0]);
}
AnnuityReader.Close();
}
private void cmbAccount_SelectedIndexChanged(object sender,EventArgs e)
{
lstAccountDetails.Items.Clear();
int index = cmbAccount.SelectedIndex;
string[] fields;
fields = Accounts[index];
lstAccountDetails.Items.Add(String.Format(“{0,10} {1,10}{2,10}”,”Rate”, “Deposit($)”, “Value($)”));
lstAccountDetails.Items.Add(String.Format(“{0,10}{1,10:C}{2,10:C}”, fields[1], double.Parse(fields[2]),double.Parse(fields[4])));
}
private void btnProcess_Click(object sender, EventArgs e)
{
lstAccountDetails.Items.Clear();
double deposit;
int index = cmbAccount.SelectedIndex;
string[] fields = Accounts[index];
StreamWriter AnnuityWriter = new StreamWriter(“annuities.txt”);
try
{
deposit = double.Parse(txtDeposit.Text);
}
catch
{
MessageBox.Show(“Enter a positive number”);
txtDeposit.SelectAll();
txtDeposit.Focus();
return;
}
double currentValue = double.Parse(fields[4]);
if (deposit > 0)
{
currentValue += deposit;
}
else
MessageBox.Show(“Please enter a positive number”);
fields[4] = currentValue.ToString();
//fields = Accounts[x];
AnnuityWriter.WriteLine(fields[0] + “,” + fields[1] + “,” +fields[3] + “,” + fields[4]);
lstAccountDetails.Items.Add(String.Format(“{0,10} {1,10}{2,10}”,”Rate”, “Deposit($)”, “Value($)”));
lstAccountDetails.Items.Add(String.Format(“{0,10}{1,10:C}{2,10:C}”, fields[1], double.Parse(fields[2]),double.Parse(fields[4])));
txtDeposit.Clear();
AnnuityWriter.Close();
}
Form1 Select Accourt Account Details: et Account Detals Process Additional Depost: Show transcribed image text Form1 Select Accourt Account Details: et Account Detals Process Additional Depost:
Expert Answer
Answer to What am I doing wrong here in this C# form? I was about 98% done with this when I encountered two issues. 1) Now I canno…