#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Need Help Changing From a Select Case


    I could use some help in changing this code from a select case to a For...Next statement or a If...Then statement, which ever seems easier? I am using VB 2010 Express. I can get the 16.50 processing fee to show in total, but none of the rest. Here is what I need help changing. This code does work, need to consolidate it. PLEASE HELP, I AM SO LOST.

    Public Class MainForm

    Private Function CalcBusiness(ByVal process1 As Double, ByVal service1 As Double, ByVal price1 As Double) As Double
    ' add basic charges for business customers to calculate the total of a cable bill

    ' declare variables
    Dim total1 As Double
    Dim processingFee As Double = 16.5
    Dim serviceFee As Double
    Dim premiumChannels As Double
    Dim connections As Double
    Dim price As Double

    ' determine the service fee for number of connections
    ' customers must have at least 1 connection
    serviceFee = connectionsListBox.SelectedIndex

    Select Case serviceFee
    Case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    connections = 80
    Case 10
    connections = 84
    Case 11
    connections = 88
    Case 12
    connections = 92
    Case 13
    connections = 96
    Case Else
    connections = 100
    End Select

    total1 = processingFee + connections + price
    totalDueLabel.Text = total1.ToString("C2")

    Return total1
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Coded using If..Then Statement


    I finally figured it out, so I am showing my results. It does work and there may be another way to do it, I am just not sure.


    'Cable Direct:


    Option Explicit On
    Option Strict On
    Option Infer On

    Public Class MainForm

    Private Function CalcBusiness(ByVal process1 As Double, ByVal service1 As Double, ByVal price1 As Double) As Double

    Dim total1 As Double
    Dim processingFee As Double = 16.5
    Dim serviceFee As Double
    Dim premiumChannels As Double
    Dim connections As Double
    Dim price As Double

    connections = connectionsListBox.SelectedIndex

    If connections >= 10 Then
    serviceFee = 80 + 4 * connections - 36
    Else
    serviceFee = 80
    End If

    premiumChannels = premiumListBox.SelectedIndex


    If premiumChannels = 0 Then
    price = 0
    Else
    price = premiumChannels * 50
    End If

    total1 = processingFee + serviceFee + price
    totalDueLabel.Text = total1.ToString("C2")

    Return total1

    End Function

    Private Function CalcResidential(ByVal process2 As Double, ByVal service2 As Double, ByVal price2 As Double) As Double

    Dim total2 As Double
    Dim processingFee As Double = 4.5
    Dim serviceFee As Double = 30
    Dim premiumChannels As Double
    Dim price As Double

    premiumChannels = premiumListBox.SelectedIndex

    If premiumChannels = 0 Then
    price = 0
    Else
    price = premiumChannels * 5.0
    End If

    total2 = processingFee + serviceFee + price
    totalDueLabel.Text = total2.ToString("C2")

    Return total2

    End Function

    Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calculateButton.Click
    If business.Checked = True Then
    Dim processingFee As Double
    Dim connections As Double
    Dim price As Double

    ' call a function procedure to perform calculations

    Call CalcBusiness(processingFee, connections, price)
    Else
    Dim processingFee As Double
    Dim serviceFee As Double
    Dim price As Double

    ' call a function procedure to perform calculations

    Call CalcResidential(processingFee, serviceFee, price)
    End If

    End Sub

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
    Me.Close()
    End Sub

    Private Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
    totalDueLabel.Text = String.Empty
    End Sub
    End Class
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    116
    Rep Power
    157
    Originally Posted by hemihead_dave
    I finally figured it out, so I am showing my results. It does work and there may be another way to do it, I am just not sure.


    'Cable Direct:


    Option Explicit On
    Option Strict On
    Option Infer On

    Public Class MainForm

    Private Function CalcBusiness(ByVal process1 As Double, ByVal service1 As Double, ByVal price1 As Double) As Double

    Dim total1 As Double
    Dim processingFee As Double = 16.5
    Dim serviceFee As Double
    Dim premiumChannels As Double
    Dim connections As Double
    Dim price As Double

    connections = connectionsListBox.SelectedIndex

    If connections >= 10 Then
    serviceFee = 80 + 4 * connections - 36
    Else
    serviceFee = 80
    End If

    premiumChannels = premiumListBox.SelectedIndex


    If premiumChannels = 0 Then
    price = 0
    Else
    price = premiumChannels * 50
    End If

    total1 = processingFee + serviceFee + price
    totalDueLabel.Text = total1.ToString("C2")

    Return total1

    End Function

    Private Function CalcResidential(ByVal process2 As Double, ByVal service2 As Double, ByVal price2 As Double) As Double

    Dim total2 As Double
    Dim processingFee As Double = 4.5
    Dim serviceFee As Double = 30
    Dim premiumChannels As Double
    Dim price As Double

    premiumChannels = premiumListBox.SelectedIndex

    If premiumChannels = 0 Then
    price = 0
    Else
    price = premiumChannels * 5.0
    End If

    total2 = processingFee + serviceFee + price
    totalDueLabel.Text = total2.ToString("C2")

    Return total2

    End Function

    Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calculateButton.Click
    If business.Checked = True Then
    Dim processingFee As Double
    Dim connections As Double
    Dim price As Double

    ' call a function procedure to perform calculations

    Call CalcBusiness(processingFee, connections, price)
    Else
    Dim processingFee As Double
    Dim serviceFee As Double
    Dim price As Double

    ' call a function procedure to perform calculations

    Call CalcResidential(processingFee, serviceFee, price)
    End If

    End Sub

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
    Me.Close()
    End Sub

    Private Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
    totalDueLabel.Text = String.Empty
    End Sub
    End Class
    you have fixated on the conditional stuff

    look at

    Code:
    If premiumChannels = 0 Then
                price = 0
            Else
                price = premiumChannels * 5.0
            End If

    the expression price=premiumChannels *5.0 is true for all values including zero so why bother with the if at all?

    the first if does not need the else you could simply front load the structure and just use an inline if

    Code:
    If connections >= 10 Then
                serviceFee = 80 + 4 * connections - 36
            Else
                serviceFee = 80
            End If
    becomes

    Code:
    serviceFee=80
    if connections >=10 then serviceFee=serviceFee+4*connections-36
    although I am not sure of the arithmatic

    80+4*connections -36 is the same as 44+4*connections

    you could look at the maths a bit more and use boolean logic to control these expressions.

    Thats a way of using the truth to add delete or multiply things by 0 or -1 false or true

    more info if needed

    Hope this helps

IMN logo majestic logo threadwatch logo seochat tools logo