Visual Basic - Select Case Problem

select case - wrong calculation - syntax wrong?

Page 1 of 1

2 Replies - 2888 Views - Last Post: 27 March 2010 - 06:12 PM Rate Topic: -----

#1 snygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-March 10

Visual Basic - Select Case Problem

Posted 27 March 2010 - 02:59 PM

I am very new to Visual basic. I don't know if I even have everything in the correct order. Here's my problem. I am trying to take user input on 3 input boxes, get discount from select case and then calculate out the following. My problem is I can get the code to do a simple addition, but If I try to do any calculation at all calling the discount I get an error where the output totals to zero. I don't know if I am missing something associated with the sub getDiscount() or if there is a problem elsewhere.

Ultimately this is what I want my calculation to end up as. Any suggestions at all on getting getDiscount() to work would be appreciated

First calculation
Price * Entered Input * Discount = Full Discount Value
Second Calculation
Price * Entered Value - Full Discount Value = Total

    Dim decGetDiscount As Decimal
    Dim decDiscountAmount As Decimal

    Public Sub GetDiscount()
        Select Case decGetDiscount
            Case Is < 10
                decDiscountAmount = 1
            Case 10 To 19
                decDiscountAmount = 0.8D
            Case 20 To 49
                decDiscountAmount = 0.7D
            Case 50 To 99
                decDiscountAmount = 0.6D
            Case Is >= 100
                decDiscountAmount = 0.5D
        End Select
    End Sub



    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear form
        txtPackageA.Clear()
        txtPackageB.Clear()
        txtPackageC.Clear()
        txtResultA.Clear()
        txtResultB.Clear()
        txtResultC.Clear()
        txtResultD.Clear()
        txtPackageA.BackColor = System.Drawing.Color.White
        txtPackageB.BackColor = System.Drawing.Color.White
        txtPackageC.BackColor = System.Drawing.Color.White
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim intPackageA As Integer
        Dim intPackageB As Integer
        Dim intPackageC As Integer
        Dim intPackageAPrice As Integer
        Dim intPackageBPrice As Integer
        Dim intPackageCPrice As Integer
        Dim decApplyDiscount As Decimal
        Dim decTotal As Decimal
        'Dim decGetDiscount As Decimal
        'Dim decDiscount As Decimal
        Dim mblnErrFlg As Boolean

        'Package prices
        intPackageAPrice = 99
        intPackageBPrice = 199
        intPackageCPrice = 299

        'Check to clear Boxes
        If mblnErrFlg = False Then
            txtPackageA.BackColor = System.Drawing.Color.White
            txtPackageB.BackColor = System.Drawing.Color.White
            txtPackageC.BackColor = System.Drawing.Color.White
            txtResultD.Clear()
        End If

        'Validate numeric input for Package A
        'intPackageA = txtPackageA.Text
        If txtPackageA.Text.Trim.Length > 0 Then
            If IsNumeric(txtPackageA.Text.Trim) = True Then
                intPackageA = Convert.ToDecimal(txtPackageA.Text.Trim)
                If intPackageA > 0 Then
                Else
                    MsgBox("Enter Numeric Greater than 0")
                    txtPackageA.BackColor = System.Drawing.Color.Yellow
                    If mblnErrFlg = False Then
                        mblnErrFlg = True
                        txtPackageA.SelectAll()
                        txtPackageA.Focus()
                    End If
                End If
            Else
                MsgBox("Enter Numeric Greater than 0")
                txtPackageA.BackColor = System.Drawing.Color.Yellow
                If mblnErrFlg = False Then
                    mblnErrFlg = True
                    txtPackageA.SelectAll()
                    txtPackageA.Focus()
                End If
            End If
        Else
            MsgBox("Enter Numeric Greater than 0")
            txtPackageA.BackColor = System.Drawing.Color.Yellow
            If mblnErrFlg = False Then
                mblnErrFlg = True
                txtPackageA.SelectAll()
                txtPackageA.Focus()
            End If
        End If

        ' Validate input for Package B
        If txtPackageB.Text.Trim.Length > 0 Then
            If IsNumeric(txtPackageB.Text.Trim) = True Then
                intPackageB = Convert.ToDecimal(txtPackageB.Text.Trim)
                If intPackageB > 0 Then
                Else
                    MsgBox("Enter Numeric Greater than 0")
                    txtPackageB.BackColor = System.Drawing.Color.Yellow
                    If mblnErrFlg = False Then
                        mblnErrFlg = True
                        txtPackageB.SelectAll()
                        txtPackageB.Focus()
                    End If
                End If
            Else
                MsgBox("Enter a Numeric Greater than 0")
                txtPackageB.BackColor = System.Drawing.Color.Yellow
                If mblnErrFlg = False Then
                    mblnErrFlg = True
                    txtPackageB.SelectAll()
                    txtPackageB.Focus()
                End If
            End If
        Else
            MsgBox("Enter Numeric Greater than 0")
            txtPackageB.BackColor = System.Drawing.Color.Yellow
            If mblnErrFlg = False Then
                mblnErrFlg = True
                txtPackageB.SelectAll()
                txtPackageB.Focus()
            End If
        End If

        'Validate input for Package C
        If txtPackageC.Text.Trim.Length > 0 Then
            If IsNumeric(txtPackageC.Text.Trim) = True Then
                intPackageC = Convert.ToDecimal(txtPackageC.Text.Trim)
                If intPackageC > 0 Then
                Else
                    MsgBox("Enter Numeric Greater than 0")
                    txtPackageC.BackColor = System.Drawing.Color.Yellow
                    If mblnErrFlg = False Then
                        mblnErrFlg = True
                        txtPackageC.SelectAll()
                        txtPackageC.Focus()
                    End If
                End If
            Else
                MsgBox("Enter Numeric Greater than 0")
                txtPackageC.BackColor = System.Drawing.Color.Yellow
                If mblnErrFlg = False Then
                    mblnErrFlg = True
                    txtPackageC.SelectAll()
                    txtPackageC.Focus()
                End If
            End If
        Else
            MsgBox("Enter Numeric Greater than 0")
            txtPackageC.BackColor = System.Drawing.Color.Yellow
            If mblnErrFlg = False Then
                mblnErrFlg = True
                txtPackageC.SelectAll()
                txtPackageC.Focus()
            End If
        End If

        GetDiscount()

'temporary calculations below, not getting anything other than addition to work

        If mblnErrFlg = False Then
            txtResultA.Text = intPackageA * intPackageAPrice * decGetDiscount
            decApplyDiscount = intPackageA * intPackageAPrice - decTotal

            txtResultB.Text = intPackageB * intPackageBPrice
            txtResultC.Text = intPackageC * intPackageCPrice

            txtResultD.Text = intPackageAPrice + intPackageBPrice + intPackageCPrice
            End If


Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic - Select Case Problem

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Visual Basic - Select Case Problem

Posted 27 March 2010 - 03:07 PM

This is VB.NET code, moved to VB.NET :)
Was This Post Helpful? 0
  • +
  • -

#3 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: Visual Basic - Select Case Problem

Posted 27 March 2010 - 06:12 PM

To start with , you never assign a value to decGetDiscount so the select case will always return a discount of 1. What does decGetDiscount represent? The total quantity of all packages or on the quantity of each individual package? In other words, if the order is for 4 of A, 4 of B and 4 of
C is the discount based on the 4's or the 12? With that answer you should be able to figure out where to put the reference to the discount logic.

There were some other minor changes that need to be made but I don't remember them all so compare your code to this. Be aware that I did make some assumptions so I might have made some changes that don't meet your need.

And don't forget that textbox.text is still text and you have to convert it to a number value before you can do math with them.

Public Class Form1

    Dim decGetDiscount As Decimal
    Dim decDiscountAmount As Decimal

    Public Sub GetDiscount()
        Select Case decGetDiscount
            Case Is < 10
                decDiscountAmount = 1
            Case 10 To 19
                decDiscountAmount = 0.8D
            Case 20 To 49
                decDiscountAmount = 0.7D
            Case 50 To 99
                decDiscountAmount = 0.6D
            Case Is >= 100
                decDiscountAmount = 0.5D
        End Select
    End Sub



    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear form 
        txtPackageA.Clear()
        txtPackageB.Clear()
        txtPackageC.Clear()
        txtResultA.Clear()
        txtResultB.Clear()
        txtResultC.Clear()
        txtResultD.Clear()
        txtPackageA.BackColor = System.Drawing.Color.White
        txtPackageB.BackColor = System.Drawing.Color.White
        Me.BackColor = System.Drawing.Color.White
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim intPackageA As Integer
        Dim intPackageB As Integer
        Dim intPackageC As Integer
        Dim intPackageAPrice As Integer
        Dim intPackageBPrice As Integer
        Dim intPackageCPrice As Integer
        Dim decApplyDiscount As Decimal
        Dim decTotal As Decimal
        Dim decGetDiscount As Decimal
        Dim decDiscount As Decimal
        Dim mblnErrFlg As Boolean

        'Package prices 
        intPackageAPrice = 99
        intPackageBPrice = 199
        intPackageCPrice = 299

        'Check to clear Boxes 
        If mblnErrFlg = False Then
            txtPackageA.BackColor = System.Drawing.Color.White
            txtPackageB.BackColor = System.Drawing.Color.White
            Me.BackColor = System.Drawing.Color.White
            txtResultD.Clear()
        End If

        'Validate numeric input for Package A 
        'intPackageA = txtPackageA.Text 
        If txtPackageA.Text.Trim.Length > 0 Then
            If IsNumeric(txtPackageA.Text.Trim) = True Then
                intPackageA = Convert.ToDecimal(txtPackageA.Text.Trim)
                If intPackageA > 0 Then
                Else
                    MsgBox("Enter Numeric Greater than 0")
                    txtPackageA.BackColor = System.Drawing.Color.Yellow
                    If mblnErrFlg = False Then
                        mblnErrFlg = True
                        txtPackageA.SelectAll()
                        txtPackageA.Focus()
                    End If
                End If
            Else
                MsgBox("Enter Numeric Greater than 0")
                txtPackageA.BackColor = System.Drawing.Color.Yellow
                If mblnErrFlg = False Then
                    mblnErrFlg = True
                    txtPackageA.SelectAll()
                    txtPackageA.Focus()
                End If
            End If
        Else
            MsgBox("Enter Numeric Greater than 0")
            txtPackageA.BackColor = System.Drawing.Color.Yellow
            If mblnErrFlg = False Then
                mblnErrFlg = True
                txtPackageA.SelectAll()
                txtPackageA.Focus()
            End If
        End If

        ' Validate input for Package B 
        If txtPackageB.Text.Trim.Length > 0 Then
            If IsNumeric(txtPackageB.Text.Trim) = True Then
                intPackageB = Convert.ToDecimal(txtPackageB.Text.Trim)
                If intPackageB > 0 Then
                Else
                    MsgBox("Enter Numeric Greater than 0")
                    txtPackageB.BackColor = System.Drawing.Color.Yellow
                    If mblnErrFlg = False Then
                        mblnErrFlg = True
                        txtPackageB.SelectAll()
                        txtPackageB.Focus()
                    End If
                End If
            Else
                MsgBox("Enter a Numeric Greater than 0")
                txtPackageB.BackColor = System.Drawing.Color.Yellow
                If mblnErrFlg = False Then
                    mblnErrFlg = True
                    txtPackageB.SelectAll()
                    txtPackageB.Focus()
                End If
            End If
        Else
            MsgBox("Enter Numeric Greater than 0")
            txtPackageB.BackColor = System.Drawing.Color.Yellow
            If mblnErrFlg = False Then
                mblnErrFlg = True
                txtPackageB.SelectAll()
                txtPackageB.Focus()
            End If
        End If

        'Validate input for Package C 
        If txtPackageC.Text.Trim.Length > 0 Then
            If IsNumeric(txtPackageC.Text.Trim) = True Then
                intPackageC = Convert.ToDecimal(txtPackageC.Text.Trim)
                If intPackageC > 0 Then
                Else
                    MsgBox("Enter Numeric Greater than 0")
                    txtPackageC.BackColor = System.Drawing.Color.Yellow
                    If mblnErrFlg = False Then
                        mblnErrFlg = True
                        txtPackageC.SelectAll()
                        Me.Focus()
                    End If
                End If
            Else
                MsgBox("Enter Numeric Greater than 0")
                Me.BackColor = System.Drawing.Color.Yellow
                If mblnErrFlg = False Then
                    mblnErrFlg = True
                    txtPackageC.SelectAll()
                    Me.Focus()
                End If
            End If
        Else
            MsgBox("Enter Numeric Greater than 0")
            Me.BackColor = System.Drawing.Color.Yellow
            If mblnErrFlg = False Then
                mblnErrFlg = True
                txtPackageC.SelectAll()
                Me.Focus()
            End If
        End If

        GetDiscount()

        'temporary calculations below, not getting anything other than addition to work 

        If mblnErrFlg = False Then
            txtResultA.Text = (intPackageA * intPackageAPrice).ToString
            'decApplyDiscount = (intPackageA * intPackageAPrice - decTotal)

            txtResultB.Text = (intPackageB * intPackageBPrice).ToString
            txtResultC.Text = (intPackageC * intPackageCPrice).ToString
            txtResultD.Text = (Decimal.Parse(txtResultA.Text) + Decimal.Parse(txtResultB.Text) + _
                               Decimal.Parse(txtResultC.Text)).ToString

            'txtResultD.Text = (intPackageAPrice + intPackageBPrice + intPackageCPrice)
        End If


    End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1