Validating combo boxes

B

Brendan

I am trying to validate the data in all of the text and combo boxes.
The text box validation seems to be working fine, but I am having the
hardest time with the combo box validation. I am not sure why, but I
keep recieving the error message:
"An unhandled exception of type 'System.NullReferenceException'
occurred in homework6.exe Additional information: Object reference
not set to an instance of an object."



here is my code:

option strict on
option explicit on

If IsNumeric(txtcustname.Text) Then
MessageBox.Show("You must enter a name without numbers",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If txtcustname.Text = "" Then
MessageBox.Show("You must enter a name", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
customer.custname = txtcustname.Text
End If
End If
If cboshirtstyle.SelectedItem.ToString = "" Then
MessageBox.Show("You must select a shirt style", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If cboshirtsize.SelectedItem.ToString = "" Then
MessageBox.Show("You must select a shirt style", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If cboshirtcolor.SelectedItem.ToString = "" Then
MessageBox.Show("You must select a shirt style", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If IsNumeric(txtshirtquantity.Text) Then
customer.shirtquantity = CDbl(txtshirtquantity.Text)
Else
If txtshirtquantity.Text = empty Then
MessageBox.Show("You must enter a shirt quantity",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

MessageBox.Show("You must enter a number for shirt
quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
customer.shirtquantity = CDbl(txtshirtquantity.Text)
End If

End If

I had the If statements set up like this:

If If cboshirtsize.SelectedItem = ""
but that gave me this error message: "Option Strict On disallows
operands of type Object for operator '='. Use the 'Is' operator to
test for object identity."
 
S

steve

homework, eh?

look at how much similar code you have...this is an "f" in maintainability.

but, to answer your question...you need to check "if cbo_ is nothing then
'do your message thing' else 'validate its data and do message thing if
needed'"...you also don't check to see if the customer object is nothing
prior to setting it's properties. finally, you should be putting this
validation logic in the customer object's 'property set' where it can throw
errors when data being set is invalid...or, at least somewhere behind an
interface (like state, isValid) that can throw needed errors prior to a save
attempt or other critical operation. code behind forms is hard to maintain
too, it does not lend itself to re-use, and should the validation criterion
change there would be much more work involved in redistributing your app.

but that's just my 0.02 usd.

hth,

steve


|I am trying to validate the data in all of the text and combo boxes.
| The text box validation seems to be working fine, but I am having the
| hardest time with the combo box validation. I am not sure why, but I
| keep recieving the error message:
| "An unhandled exception of type 'System.NullReferenceException'
| occurred in homework6.exe Additional information: Object reference
| not set to an instance of an object."
|
|
|
| here is my code:
|
| option strict on
| option explicit on
|
| If IsNumeric(txtcustname.Text) Then
| MessageBox.Show("You must enter a name without numbers",
| "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
| Else
| If txtcustname.Text = "" Then
| MessageBox.Show("You must enter a name", "Error",
| MessageBoxButtons.OK, MessageBoxIcon.Error)
| Else
| customer.custname = txtcustname.Text
| End If
| End If
| If cboshirtstyle.SelectedItem.ToString = "" Then
| MessageBox.Show("You must select a shirt style", "Error",
| MessageBoxButtons.OK, MessageBoxIcon.Error)
| End If
| If cboshirtsize.SelectedItem.ToString = "" Then
| MessageBox.Show("You must select a shirt style", "Error",
| MessageBoxButtons.OK, MessageBoxIcon.Error)
| End If
| If cboshirtcolor.SelectedItem.ToString = "" Then
| MessageBox.Show("You must select a shirt style", "Error",
| MessageBoxButtons.OK, MessageBoxIcon.Error)
| End If
| If IsNumeric(txtshirtquantity.Text) Then
| customer.shirtquantity = CDbl(txtshirtquantity.Text)
| Else
| If txtshirtquantity.Text = empty Then
| MessageBox.Show("You must enter a shirt quantity",
| "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
|
| MessageBox.Show("You must enter a number for shirt
| quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
| Else
| customer.shirtquantity = CDbl(txtshirtquantity.Text)
| End If
|
| End If
|
| I had the If statements set up like this:
|
| If If cboshirtsize.SelectedItem = ""
| but that gave me this error message: "Option Strict On disallows
| operands of type Object for operator '='. Use the 'Is' operator to
| test for object identity."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top