Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am programming a C sharp application, I am using Windows Forms method The appl

ID: 3765326 • Letter: I

Question

I am programming a C sharp application, I am using Windows Forms method

The application is running, but I get an error message in the begenning and after the error message the program runs fine.

The error : "Values are either too small or too large int32"

why I get the error message.

source 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;
using System.IO;

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

private void checkAccountsButton_Click(object sender, EventArgs e)
{
List accountList = new List();
int accountNumber = 0;

try
{
if (int.Parse(accountsTextBox.Text) > 0)
{
accountNumber = int.Parse(accountsTextBox.Text);
}
else
{
MessageBox.Show("Enter positive number");
}
}

catch
{

MessageBox.Show("Enter the numeric account number");


}

try
{
StreamReader InputFile = File.OpenText("AccountList.txt");

while (!InputFile.EndOfStream)
{
accountList.Add(int.Parse(InputFile.ReadLine()));
}

InputFile.Close();

}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

try
{
bool AccountFound = false;

foreach (int account in accountList)
{
if (account == accountNumber)
{
AccountFound = true;
}
}
if (AccountFound)
{
MessageBox.Show("Valid Account Number");
}
else
{
MessageBox.Show("Invalid Account Number, try again");
}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}
}

Explanation / Answer

The .NET Framework provides a method called Int32.Parse that you can use to convert a string value to an integer if you need to perform arithmetic computations on values held as strings.

Convert a string to an int

Call the System.Int32.Parse method.

For example:
string stringVar = "42";
int intVar = System.Int32.Parse(stringVar);

Locate the calculateClick method, and look at the first two statements of this method after the try statement and opening brace.
These statements look like this:
int leftHandSide = System.Int32.Parse(lhsOperand.Text);
int rightHandSide = System.Int32.Parse(rhsOperand.Text);


These two statements declare two int variables, called leftHandSide and rightHandSide. Notice the way in which the variables are initialized. In both cases, the Parse method of the System.Int32 class is called. (System is a namespace, and Int32 is the name of the class in this namespace.)

You have seen this method before—it takes a single string parameter and converts it to an int value. These two lines of code take whatever the user has typed into the lhsOperand and rhsOperand text box controls on the form and converts them to int values.

Ensure that integer arithmetic is always checked for overflow

Use the checked keyword. For example:
int number = Int32.MaxValue;
checked
{
number++;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote