Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can\'t get my code below to work with Option Strict On due to this part of the

ID: 3666666 • Letter: I

Question

I can't get my code below to work with Option Strict On due to this part of the code "partscost = parts" due to string to integer conversion.

It is the only part of the code that won't work - how I can I make it work with Option Strict On?


Public Class Form2
    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TxtJobNumber.Text = ""
        TxtName.Text = ""
        TxtParts.Text = ""
        TxtHours.Text = ""

    End Sub

    Private Sub BtnOk_Click(sender As Object, e As EventArgs) Handles BtnOk.Click
        Me.Close()
    End Sub

    'Declare Constants
    Const LABOR_COST As Currency = 50
    Const SALES_TAX As Currency = 0.08

    Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
        Dim parts As String
        Dim laborhrs As String
        Dim laborcharges As Integer
        Dim partscost As Integer
        Dim subtotal As Integer
        Dim total As Integer
        Dim tax As Integer

        parts = TxtParts.Text
        laborhrs = TxtHours.Text
        laborcharges = LABOR_COST * laborhrs
        partscost = parts
        tax = SALES_TAX * parts
        subtotal = partscost + laborcharges
        total = subtotal + tax

        TxtPartsCharges.Text = partscost.ToString("C")
        TxtLaborCharges.Text = laborcharges.ToString("C")
        TxtSalesTax.Text = tax.ToString("C")
        TxtSubTotal.Text = subtotal.ToString("C")
        TxtTotal.Text = total.ToString("C")


    End Sub
End Class

Explanation / Answer

Firstly, if you look through the code you would come to know that

parts -  Dim parts As String - is of string type

partscost - Dim partscost As Integer - is of integer part

and with the strict option all the widening coversion are stopped, so either you have to change the data type or provide an explicit conversion

For more : https://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote