I'm trying to get a user input number from text box on my GUI, then there are some calculation for that number and display it later in the same text box. Number entered is supposed to have hyphens which are then stripped to check for validity of that ISBN number. My ISBNChecker class strips hyphens, then calculates the check digit as follows.

``````public void stripHyphens()
{
numberEntered = numberEntered.Replace("-", "");
number = Convert.ToInt64(numberEntered);
}
public void separateDigits ()
{

if (numberEntered.Length == 10)
{
checkDigit = Convert.ToInt16(this.number % 10);
number = number / 10;
}

// logic for either 9-or 10- digit number
digit9 = Convert.ToInt16(this.number % 10); //this means class level not local level
number = number / 10;

digit8 = Convert.ToInt16(this.number % 10);
number = number / 10;

digit7 = Convert.ToInt16(this.number % 10);
number = number / 10;

digit6 = Convert.ToInt16(this.number % 10);
number = number / 10;

digit5 = Convert.ToInt16(this.number % 10);
number = number / 10;

digit4 = Convert.ToInt16(this.number % 10);
number = number / 10;

digit3 = Convert.ToInt16(this.number % 10);
number = number / 10;

digit2 = Convert.ToInt16(this.number % 10);
number = number / 10;

digit1 = Convert.ToInt16(this.number % 10);

} // end method separateDigits

public void calculateCheckDigit()
{
sumOfDigits = ((digit1 * 10) + (digit2 * 9) + (digit3 * 8) + (digit4 * 7) + (digit5 * 6) +
(digit6 * 5) + (digit7 * 4) + (digit8 * 3) + (digit9 * 2));
Console.WriteLine("sum: " + sumOfDigits);
Console.WriteLine("The check digit is: " + displayCheckDigit()); //wrapping method inside the method - implicit conversion

} //end method calculateCheckDigit``````

My GUI so far checks two methods:

``````namespace ISBNChecker
{

public partial class ISBNGUI : Form
{
ISBNChecker check = new ISBNChecker();

public ISBNGUI()
{
InitializeComponent();

}

private void xValidatebutton_Click(object sender, EventArgs e)
{
check.setNumberEntered("test");
check.inspectNumber();
}``````

The problem is I know there is a data type confusion in the NumberEntered parameter, but I don't know how to fix this. So far, it's grabbing the "test" value all the time. I tried to leave this blank "", it crashed. Can somebody please help me? I am really not sure where I am supposed to change this data type from string to long. I've tried to write a separate method to convert NumberEntered to long, that did not work either.

You cannot always convert an Int64 to an Int16.
An Int16 can not be greater than 32768.

It has to be Int64, because the user enters a 10 digit ISBN number which is then stripped down to individual digit to do math to check for validity.
Any suggestions?
Thanks

C# input string not in correct format error - You are trying to convert non-numeric string value into int.
Use TryParse method instead of Convert.ToIntXX.

``````int no;
string data="100a";
int.TryParse(data, out no);``````

Could you exactly point out in your code where it fails?
Eventually sending us a zip of your project?
I see you are also mixing two UI's, nothing wrong with that if you are debugging, but you should avoid it in your final project.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.