Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 11th, 2006, 4:41 AM   #1
jaybird01
Newbie
 
Join Date: Aug 2006
Posts: 4
Rep Power: 0 jaybird01 is on a distinguished road
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
jaybird01 is offline   Reply With Quote
Old Aug 11th, 2006, 6:38 AM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Welcome to the forums.

When you say 'horribly off track', are you suggesting that it's producing wrong results?
OpenLoop is offline   Reply With Quote
Old Aug 11th, 2006, 6:59 AM   #3
jaybird01
Newbie
 
Join Date: Aug 2006
Posts: 4
Rep Power: 0 jaybird01 is on a distinguished road
Thank you for your prompt reply. When I run my program, this is the error message:
Quote:
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Operator is not valid for Char and Char.
I am not certain whether it was correct to initialize my index and also whether I am using the correct code within the loop for lst/cbo item selection or whether this is the correct loop to use. Any ideas?

Thanks
jaybird01 is offline   Reply With Quote
Old Aug 11th, 2006, 8:22 AM   #4
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 242
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
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
melbolt is offline   Reply With Quote
Old Aug 11th, 2006, 9:21 AM   #5
jaybird01
Newbie
 
Join Date: Aug 2006
Posts: 4
Rep Power: 0 jaybird01 is on a distinguished road
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?
jaybird01 is offline   Reply With Quote
Old Aug 11th, 2006, 10:01 AM   #6
john Wesley
Hobbyist Programmer
 
john Wesley's Avatar
 
Join Date: May 2006
Location: United Kingdom
Posts: 119
Rep Power: 3 john Wesley is on a distinguished road
Send a message via MSN to john Wesley Send a message via Yahoo to john Wesley
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 If

It 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..
john Wesley is offline   Reply With Quote
Old Aug 11th, 2006, 10:42 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 14th, 2006, 5:35 AM   #8
jaybird01
Newbie
 
Join Date: Aug 2006
Posts: 4
Rep Power: 0 jaybird01 is on a distinguished road
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 Sub

Thanks 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
jaybird01 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC