0

I don't like to admit it but I am building a vb.net winform that requires the use of reading text fields, check boxes or radio buttons to build a sql query and return results. the sql query part works fantastically however the select case statements don't seem to be evaluating the checkbox or radio checkstate correctly.

Select Case True
Case txtFirstName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text)
    dgvEmployees.Visible = True
Case txtFirstName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text, , , "Active")
    chkInactive.Enabled = False
    dgvEmployees.Visible = True
Case txtFirstName.Text.Length > 0 & chkInactive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text, , , "Inactive")
    chkActive.Enabled = False
    dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text)
    dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text, , "Active")
    chkInactive.Enabled = False
    dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkInactive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text, , "Inactive")
    chkActive.Enabled = False
    dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & rdbActive.Checked
    dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text)
    dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & chkActive.Checked
    dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text, "Active")
    chkInactive.Enabled = False
    dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & chkInactive.CheckState = CheckState.Checked
    dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text, "Inactive")
    chkActive.Enabled = False
    dgvEmployees.Visible = True
Case Else
    MessageBox.Show("please enter an employee code, firstname or lastname")
End Select

Every time I debug i get an invalid cast exception for the radio button or check box, am I missing something?

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 2
    `&` is not the right thing to use. `AndAlso` performs a logical `And` – Ňɏssa Pøngjǣrdenlarp Aug 10 '17 at 14:19
  • Maybe I am missing something but, arent the tests for checks 1 and 2 identical? Same with 3 and 4. Since the text length tests is the same everywhere, that could be done once. – Ňɏssa Pøngjǣrdenlarp Aug 10 '17 at 14:28
  • @Plutonix Thanks for the help. I didn't know it followed a similar syntax as vb6. I was under the impression that it would follow C# syntax....as ignorant as that may sound. – Robert DeSautel Aug 10 '17 at 14:29
  • @Plutonix no they use different parameter of another function because I have created a query can will search either textbox for a result...I have 3 different textboxes that build a single query to return a table result in data grid view....If you have a more efficient way to doing it I am open to suggestions....I normally write with C# – Robert DeSautel Aug 10 '17 at 14:32
  • `Case txtFirstName.Text.Length > 0 & chkActive.CheckState` is the same test for #1 and #2. The code for the second test is never going to run – Ňɏssa Pøngjǣrdenlarp Aug 10 '17 at 14:35
  • @Plutonix Oh I didn't see that....thanks for pointing that out for me I will fix that. I appreciate the help – Robert DeSautel Aug 10 '17 at 14:43
  • 2
    First off, set `Option Strict On`. That way you would have never been able to compile that code and the IDE would have guided you to the problems. You are also using a mix of `Checked` and `CheckedState`. Some usages are correct (other than the "&") and others are not. – TnTinMn Aug 10 '17 at 14:59
  • @TnTinMn Thanks for the input. I will look at the options strict setting on my project and ensure that it is on. I assumed that it was already on and I hope it does help me in the long run so I don't have such trivial issues. I had those different type of usages because I was trying to figure out why it was working. Thanks for the help and advice tntinMn – Robert DeSautel Aug 10 '17 at 15:08

1 Answers1

2

In Visual Basic, & performs a string concatenation. You want to use And or AndAlso, as was mentioned by Plutonix.

AndAlso would be the better choice, as it skips evaluating the second condition if the first fails. For example, if txtFirstName.Text has a length of 0, the program would not bother to check the state of chkActive.CheckState. Actually, because of this, it might be theoretically a tiny bit more efficient to do check chkActive.CheckState first, because that is probably a little faster to evaluate.

nayrangnu
  • 103
  • 5
  • 1
    Thanks for the feedback nayrangnu. That is something will remember from now on. I appreciate the help. And thanks for the evaluation tip I will make that change. – Robert DeSautel Aug 10 '17 at 14:46