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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.