![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2006
Posts: 4
Rep Power: 0
![]() |
VB.NET: Loops - calculate selected items
Dear Members
I am new to this forum and I am very excited to join you and learn from you and I hope able to share from my knowledge. I have a problem with the following code which I extracted from my program. I have been going well until this section of my program and I hope someone is able to assist me. Basically, this section of my program should calculate selected list items within a loop. I have a feeling I am horribly off track. Could someone please give me an idea where I am going wrong as I am striving to make my programs as efficient as possible. I look forward to your response. Thanking you in anticipation Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
'Calculate Yacht Charges based on rates
Dim intIndex As Integer = 0
Dim intHrsCharted As Integer
Dim decPrice As Decimal
Dim decItemAmount As Decimal
Dim strMessage As String
Do While intIndex < lstYachtSize.Items.Count
If lstYachtSize.SelectedItem(0) And cboYachtType.SelectedItem(0) Then
decPrice = mdecSize22_RATE
ElseIf lstYachtSize.SelectedItem(1) And cboYachtType.SelectedItem(1) Then
decPrice = mdecSize24_RATE
ElseIf lstYachtSize.SelectedItem(2) And cboYachtType.SelectedItem(2) Then
decPrice = mdecSize30_RATE
ElseIf lstYachtSize.SelectedItem(3) And cboYachtType.SelectedItem(3) Then
decPrice = mdecSize32_RATE
ElseIf lstYachtSize.SelectedItem(4) And cboYachtType.SelectedItem(4) Then
decPrice = mdecSize36_RATE
ElseIf lstYachtSize.SelectedItem(5) And cboYachtType.SelectedItem(5) Then
decPrice = mdecSize38_RATE
ElseIf lstYachtSize.SelectedItem(6) And cboYachtType.SelectedItem(6) Then
decPrice = mdecSize45_RATE
Else
MessageBox.Show("Item is not in the list", "No item match", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
'Calculate extended price and add to order total
If txtHrsCharted.Text <> "" Then 'Not blank
If IsNumeric(txtHrsCharted.Text) Then 'Is numeric
Try
intHrsCharted = CInt(txtHrsCharted.Text)
decItemAmount = decPrice * intHrsCharted
mdecTotal += decItemAmount
lblTotalCharges.Text = FormatCurrency(decItemAmount)
Catch ex As Exception
strMessage = "Calculation error."
MessageBox.Show(strMessage, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else 'Missing data
strMessage = "Enter the quantity."
MessageBox.Show(strMessage, "Data entry erro", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Loop
End Sub |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Welcome to the forums.
When you say 'horribly off track', are you suggesting that it's producing wrong results? |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Aug 2006
Posts: 4
Rep Power: 0
![]() |
Thank you for your prompt reply. When I run my program, this is the error message:
Quote:
Thanks |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
first off, which line is it blowing up on? insert a breakpoint on the line before that and see what the values in your variables involved in the crash are.
sounds like you're trying to put something bad into an integer.
__________________
I have never let my schooling interfere with my education. -Mark Twain- Xbox live gamertag: melbolt |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Aug 2006
Posts: 4
Rep Power: 0
![]() |
My program starts hanging on this line ...
If lstYachtSize.SelectedItem(0) And cboYachtType.SelectedItem(0) Then The variables indicate "22" for the yacht size and "C&C" for the yacht type. So, if the user selects a yacht size and type, the calculation for these 2 selections should place the total in a label. Any ideas as to what I am doing wrong? |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
This has a chance to be of no use but still, I found the way things are linked in vb using 'and', 'and also' etc sometimes can have issues that are not of much concern, Dont know why but it seems to spit perfectly sensible code back at you. so out of curiosity try this :
Do While intIndex < lstYachtSize.Items.Count
If lstYachtSize.SelectedItem(0)Then
If cboYachtType.SelectedItem(0) Then
decPrice = mdecSize22_RATE
End If
End IfIt makes for repetitive code but beats drinking coffee and staring at whats already there.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I would suggest that "C&C" is not a variable that is amenable to calculation of a total. That seems to be what you're suggesting.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Aug 2006
Posts: 4
Rep Power: 0
![]() |
Loops
Dear Members
I have finally managed to get the code to work. I was very silly as it was not logical to place the code within a loop. The program does not require a repetition of actions nor does it search for items in a list. I therefore did not require the loop as I had already selected the item and then needed to do the calculation. Also, the calculation really has nothing to do with the selection of yacht type. I guess I needed the weekend break to see all this logic. See correct code below: Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
'Calculate Yacht Charges based on rates
Dim intIndex As Integer = 0
Dim intHrsCharted As Integer
Dim decPrice As Decimal
Dim decItemAmount As Decimal
Dim strMessage As String
If cboYachtType.SelectedIndex = 0 Then
decPrice = mdecSize22_RATE
ElseIf cboYachtType.SelectedIndex = 1 Then
decPrice = mdecSize24_RATE
ElseIf cboYachtType.SelectedIndex = 2 Then
decPrice = mdecSize30_RATE
ElseIf cboYachtType.SelectedIndex = 3 Then
decPrice = mdecSize32_RATE
ElseIf cboYachtType.SelectedIndex = 4 Then
decPrice = mdecSize36_RATE
ElseIf cboYachtType.SelectedIndex = 5 Then
decPrice = mdecSize38_RATE
ElseIf cboYachtType.SelectedIndex = 6 Then
decPrice = mdecSize45_RATE
Else
MessageBox.Show("Item is not in the list", "No item match", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
'Calculate extended price and add to order total
If txtHrsCharted.Text <> "" Then 'Not blank
If IsNumeric(txtHrsCharted.Text) Then 'Is numeric
Try
intHrsCharted = CInt(txtHrsCharted.Text)
decItemAmount = decPrice * intHrsCharted
mdecTotal += decItemAmount
lblTotalCharges.Text = FormatCurrency(decItemAmount)
Catch ex As Exception
strMessage = "Calculation error."
MessageBox.Show(strMessage, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else 'Missing data
strMessage = "Enter the quantity."
MessageBox.Show(strMessage, "Data entry error", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End SubThanks once again for your patience and all your wonderful input. I hope I am able to assist any other novices like myself in this forum. Best Regards |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|