I am a recent college graduate and I hope to use this forum to keep my skills sharp.
I am making a program to calculate depreciation (just to apply what I learned) and when I run the program and made the input, I get an error that says "Please input numeric values." Here is the code for the calculate button (If you need me to post anything else, please say so) :
Code:
Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
'Declaring variables for this button.
Dim InitialCost, Salvage, LifeInYears As Integer
Dim DepreciableCost, YearlyDepreciation As Decimal
Try
'Convert input values to numeric variables.
InitialCost = Integer.Parse(CostTextBox.Text)
Salvage = Integer.Parse(SalvageTextBox.Text)
LifeInYears = Integer.Parse(LifeTextBox.Text)
DepreciableCost = Decimal.Parse(DepreciableCostTextBox.Text)
YearlyDepreciation = Decimal.Parse(YearlyDepreciationTextBox.Text)
'Calculate numeric values.
DepreciableCost = InitialCost - Salvage
YearlyDepreciation = DepreciableCost / LifeInYears
'Display output
If DepreciableCost <= 0 Then
MessageBox.Show("Depreciable cost cannot be a negative number.", "Seriously?!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
DepreciableCostTextBox.Text = DepreciableCost.ToString("N2")
YearlyDepreciationTextBox.Text = YearlyDepreciation.ToString("N2")
End If
Catch ex As Exception
MessageBox.Show("Please input numeric values.", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
This exception is to make sure that the user inputs numbers and not non-numbers such as letters. If you require to see the form and the whole code for that form, I'll provide it now. Also this form is to calculate the Straight-Line method of depreciation: Cost - Salvage / Life.
Code:
Public Class StraighLineForm
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
MainForm.Show()
End Sub
Private Sub EraseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EraseButton.Click
CostTextBox.Clear()
SalvageTextBox.Clear()
LifeTextBox.Clear()
YearlyDepreciationTextBox.Clear()
DepreciableCostTextBox.Clear()
End Sub
Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
'Declaring variables for this button.
Dim InitialCost, Salvage, LifeInYears As Integer
Dim DepreciableCost, YearlyDepreciation As Decimal
Try
'Convert input values to numeric variables.
InitialCost = Integer.Parse(CostTextBox.Text)
Salvage = Integer.Parse(SalvageTextBox.Text)
LifeInYears = Integer.Parse(LifeTextBox.Text)
DepreciableCost = Decimal.Parse(DepreciableCostTextBox.Text)
YearlyDepreciation = Decimal.Parse(YearlyDepreciationTextBox.Text)
'Calculate numeric values.
DepreciableCost = InitialCost - Salvage
YearlyDepreciation = DepreciableCost / LifeInYears
'Display output
If DepreciableCost <= 0 Then
MessageBox.Show("Depreciable cost cannot be a negative number.", "Seriously?!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
DepreciableCostTextBox.Text = DepreciableCost.ToString("N2")
YearlyDepreciationTextBox.Text = YearlyDepreciation.ToString("N2")
End If
Catch ex As Exception
MessageBox.Show("Please input numeric values.", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class
You are just catching any exception in your Try/Catch block, but you do not know what exception is being thrown therefore, you, I or anybody else is not going to be able to tell what is the source of your error.
A quick thing to do would be to either but the ex.message in your MessageBox.Show.
Or you could run your code outside of the Try/Catch block and use the debugger to see what line of code fails.
...
If someone helps you, show it by rating their post!
VB.net Code:
' These two lines will make your coding life much easier!
Option Explicit On
Option Strict On
"Check everything. That's what software developers do." jmcilhinney
I declared the variables to be calculated as global variables instead of local variables and the green lines vanished for all of them. Now the only issue left is displaying the output. The depreciable cost cannot be 0 or less.
Code:
'Convert input values to numeric variables.
InitialCost = CostTextBox.Text
Salvage = SalvageTextBox.Text
LifeInYears = LifeTextBox.Text
DepreciableCost = DepreciableCostTextBox.Text
YearlyDepreciation = YearlyDepreciationTextBox.Text
'Calculate numeric values.
DepreciableCost = InitialCost - Salvage
YearlyDepreciation = DepreciableCost / LifeInYears
'Display output
If DepreciableCost <= 0 Then
MessageBox.Show("Depreciable cost cannot be 0 or a negative number.", "Seriously?!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
DepreciableCostTextBox.Text = DepreciableCost.ToString("N2")
YearlyDepreciationTextBox.Text = YearlyDepreciation.ToString("N2")
End If
End Sub
End Class
And you're right, there's still some input validation to be done. When I put "a" in one of the text boxes, the debug pointed to this as "the following exception was unhandled".
Code:
If LifeTextBox.Text = 0 Then
MsgBox("Life cannot be 0. Try Again.")
End If
True, but for how much longer?
Better to learn the uptodate methods now. In the future Microsoft could remove legacy support + you'll need to learn the current methods anyway.
if integer.tryparse(CostTextBox.Text, InitialCost) then
'textbox contains an integer
'InitialCost now equals that number
end if
all of the other numeric datatypes also have a tryparse method
I'm so sorry for not replying in so long, but your response worked after taking a long time to figure it out.
Code:
'avoiding bad input
If Integer.TryParse(CostTextBox.Text, InitialCost) Then
InitialCost = Integer.Parse(CostTextBox.Text)
End If
If Integer.TryParse(SalvageTextBox.Text, Salvage) Then
Salvage = Integer.Parse(SalvageTextBox.Text)
End If
If Integer.TryParse(LifeTextBox.Text, LifeInYears) Then
LifeInYears = Integer.Parse(LifeTextBox.Text)
End If
If Integer.TryParse(DepreciableCostTextBox.Text, DepreciableCost) Then
DepreciableCost = Decimal.Parse(DepreciableCostTextBox.Text)
End If
If Integer.TryParse(YearlyDepreciationTextBox.Text, YearlyDepreciation) Then
YearlyDepreciation = Decimal.Parse(YearlyDepreciationTextBox.Text)
End If
The thing about inputting non-numeric characters means that the app will record it as 0; that's better than the app crashing though. Thanks so much for this.
as the 2nd parameter (InitialCost) is passed ByRef
When I tried to do your suggested code for the output boxes(DepreicableCostTextBox.Text and YearlyDepreciationTextBox.Text), it crashed and I got this when I ran it.
For the time being, I am done with the Straight Line method; I have moved on to the Units Of Production method. Here is the code:
Code:
Private Sub CalculateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
'Declaring variables
Dim InitialCost As Integer = 0
Dim Salvage As Integer = 0
Dim TotalUnitsExpected As Integer = 0
Dim UnitsProductionInPeriod As Integer = 0
Dim DepreciableCost As Decimal = 0
Dim DepreciationPerUnit As Decimal = 0
Dim DepreciationForPeriod As Decimal = 0
'Validating data
Integer.TryParse(InitialCostTextBox.Text, InitialCost)
Integer.TryParse(SalvageTextBox.Text, Salvage)
Integer.TryParse(TotalUnitsExpectedTextBox.Text, TotalUnitsExpected)
Integer.TryParse(UnitsProductionInPeriodTextBox.Text, UnitsProductionInPeriod)
Decimal.TryParse(DepreciationPerUnitTextBox.Text, DepreciationPerUnit)
Decimal.TryParse(DepreciationForPeriodTextBox.Text, DepreciationForPeriod)
'Calculate depreicable cost
DepreciableCost = InitialCost - Salvage
'Calculate depreciation per unit; cannot divide by 0
If TotalUnitsExpected = 0 Then
MsgBox("Cannot divide by 0.")
Else
DepreciationPerUnit = DepreciableCost / TotalUnitsExpected
End If
'Calculate Depreciation for period
DepreciationForPeriod = DepreciationPerUnit * UnitsProductionInPeriod
'Display output
DepreciationPerUnitTextBox.Text = DepreciationPerUnit.ToString("N2")
DepreciationForPeriodTextBox.Text = DepreciationForPeriod.ToString("N2")
Here is the test data. When I multiply 5.29 by 4500, I get 23,805 on my calculator, but when I divide the salvage (18500) by the total units expected (3500), I get a long decimal; when I multiply that long decimal by 4500, I get 23785.71.