|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Interesting Survey Program, Can't See Bug...
So I've been working on some programming using Visual Basic, and have had no problems with debugging and testing small programs, except for this one.
First off, the code: Code:
Public Class SiblingSurveyForm
' display what siblings user selects
Private Sub submitButton_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles submitButton.Click
' check if user selects brothers or sisters
' and no siblings
If (noneCheckBox.Checked = True AndAlso _
brotherCheckBox.Checked = True AndAlso _
sisterCheckBox.Checked = True) Then
MessageBox.Show("Selected combination is not possible", _
"Invalid Input", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
' check if user selects brothers and no siblings
ElseIf brotherCheckBox.Checked = True AndAlso _
noneCheckBox.Checked = True Then
MessageBox.Show("Selected combination is not possible", _
"Invalid Input", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
' check if user selects sisters and no siblings
ElseIf sisterCheckBox.Checked = True AndAlso _
noneCheckBox.Checked = True Then
MessageBox.Show("Selected combination is not possible", _
"Invalid Input", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
' check if user has brothers and sisters
ElseIf brotherCheckBox.Checked = True OrElse _
sisterCheckBox.Checked = True AndAlso _
noneCheckBox.Checked = False Then
MessageBox.Show("You have brothers and sisters", _
"Siblings", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
' check if user has brothers
ElseIf brotherCheckBox.Checked = True AndAlso _
sisterCheckBox.Checked = False AndAlso _
noneCheckBox.Checked = False Then
MessageBox.Show("You have at least one brother", _
"Siblings", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
' check if user has sisters
ElseIf sisterCheckBox.Checked = True AndAlso _
brotherCheckBox.Checked = False AndAlso _
noneCheckBox.Checked = False Then
MessageBox.Show("You have at least one sister", _
"Siblings", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
' user has no siblings
Else
MessageBox.Show("You have no siblings", _
"Siblings", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
End Sub ' submitButton_Click
End Class ' SiblingSurveyForm
The program is to determine, based upon CheckBoxes, if the user has at least one brother, or a sister, or a combination of both. The program is to also generate an error to the user if either the Brother / Sister CheckBoxes are checked, along with the No Siblings CheckBox. Those work fine. My issue is, when just Brother or Sister are selected, the program is to generate a message saying that the user at least has one brother or sister... the message appears saying that the user has both brothers and sisters. Does anyone see an issue with the code that I can't see? I have tried various logical operators to see if I can have the program test for the right result, based upon what CheckBox is checked or not. Thanks!
__________________
-Jason Clark |
|
#2
|
||||
|
||||
|
Quote:
I think you should check out the short circuiting you got going on for one thing. You probably want the old fashioned If (((Brother) Or (Sister)) And Not (None)) You can stop testing for None with that Code:
If (((Brother) Or (Sister)) And Not (None)) Then
If (Brother) Then
If (Sister) Then
' You have both
Else
' You have brother
EndIf
Else
' You have sister
EndIf
Else
' Not valid
EndIf
__________________
medialint.com "Beware of the man who works hard to learn something, learns it, and finds himself no wiser than before. He is full of murderous resentment of people who are ignorant without having come by their ignorance the hard way." - Vonnegut - Cat's Cradle, 1963 |
|
#3
|
|||
|
|||
|
So in other words, shortening the If... ElseIf statements I have will display my results more accurately?
I'll give it a shot and report back. |
|
#4
|
|||
|
|||
|
And fixed.
There was a logical operator in the "brothers and sisters" section of the ElseIf statements that was throwing the program off. Changed the "OrElse" entry to an "AndAlso", and it runs exactly how I want it. Medialint, I did try the suggestion you had, and I'm not sure if it was just the way I was coding things, but I was getting errors left and right, and still was not working the way I wanted it to. If you want to explain it further, I would be willing to see the logic you had thought of behind your suggestion though. |
|
#5
|
||||
|
||||
|
That's mostly a pseudocode mockup I thought it should be fairly clear. You don't need to test for each of permutations.
Last edited by medialint : August 12th, 2008 at 10:11 AM. |
|
#6
|
|||
|
|||
|
Quote:
I'll give it another shot when I have the chance. |
|
#7
|
||||
|
||||
|
You might want to consider the non-short-circuiting versions And and Or instead of the short-circuiting versions AndAlso and OrElse. When using the short-circuiting, you have to be a little more careful with the order in which conditions are checked so that short-circuiting doesn't skip something (such as checking the value of the 'none' checkbox) that it shouldn't ever skip.
When you have X Or Y, both X and Y will be evaluated. When you have X OrElse Y, then first X is evaluated. If it is True, then it doesn't matter whether Y evaluates to True or False, so it doesn't look at Y; it short-circuits and evaluates as True. When you have X And Y, both X and Y will be evaluated. When you have X AndAlso Y, then first X is evaluated. If it is False, then it doesn't matter whether Y evaluates to True or False, so it doesn't look at Y; it short-circuits as False.
__________________
Joel B Fant "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that." .NET Must-Haves Tools: Reflector References: Threading in .NET |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Interesting Survey Program, Can't See Bug... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|