-1
myString = txtEmployeeID.Text;// storing what the user enters into myString.

tempObject = new EmployeeClass();

tempObject.getAccessID(myString);

I get an error on this line

How do I fix line 3?

Hussein Salman
  • 7,806
  • 15
  • 60
  • 98
jaysun
  • 1
  • 3

1 Answers1

0

You need to convert the string to number (an integer probably) as that is what the getAccessID method expects.

myString = txtEmployeeID.Text;
tempObject = new EmployeeClass();
int number = 0;
bool isNumber = Int32.TryParse(myString, out number);
if (isNumber) tempObject.getAccessID(number);

Tip: Always give functions/methods the parameters they expect, in the correct datatype.


An alternate method is to only accept digits in the textbox:

private void txtEmployeeID_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
    {
            e.Handled = true;
    }

    // only allow one decimal point
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
}

Make it only accept > int.MinValue and < int.MaxValue then you can safely do this:

tempObject.getAccessID(Convert.ToInt32(txtEmployeeID.Text));
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321