# Tax Calculator in Visual Basic

### Question

• I am trying to write code for  a tax calculator.

I take the users taxable income and then display their tax liability, percentage of their income, and marginal tax bracket.

for each bracket I am given a different base rate and percentage of excess.

Here is my code so far.....

If Not IsNumeric(txtIncome.Text) Then
End If
Dim income As Double = CDbl(txtIncome.Text)
Dim liabilityTax, taxBracket, percentTax As Double

Select Case income
Case CDbl((income) <= 8700)
liabilityTax = (income * 0.1)
taxBracket = (0)
percentTax = (liabilityTax) / income

Case CDbl(income <= 35350)
liabilityTax = (870 + (0.15 * (income - 8700)))
taxBracket = 15
percentTax = (liabilityTax) / income

Case CDbl(income <= 85650)
liabilityTax = (4867.5 + (0.25 * (income - 35350)))
taxBracket = 28
percentTax = (liabilityTax) / income

Case CDbl(income <= 178650)
liabilityTax = (17442.5 + (0.28 * (income - 85650)))
taxBracket = 33
percentTax = (liabilityTax) / income

Case CDbl(income <= 388350)
liabilityTax = (43482.5 + (0.33 * (income - 178650)))
taxBracket = 35
percentTax = (liabilityTax) / income

Case CDbl(income > 388350)
liabilityTax = (112683.5 + (0.35 * (income - 388350)))
taxBracket = 38
percentTax = (liabilityTax) / income

End Select
txtLiability.Text = FormatCurrency(liabilityTax)
txtBracket.Text = FormatPercent(taxBracket)
txtPercent.Text = FormatCurrency(percentTax)

Whenever I run the program i end up with 0's for each category. I do not know where I am going wrong.

If anyone can help that would be greatly appreciated!

Saturday, September 29, 2012 7:27 PM

• Here is an example that may correct some of the issues in your code. (there are others). I changed some items instead of setting up TextBoxes etc. but you will get the idea.

```       Dim txtIncome As String = "388350.88"
Dim income As Double = CDbl(txtIncome)
Dim liabilityTax, taxBracket, percentTax As Double
Select Case income
Case Is <= 8700
liabilityTax = income * 0.1
taxBracket = 0
percentTax = liabilityTax / income
Case Is <= 35350
liabilityTax = (870 + (0.15 * (income - 8700)))
taxBracket = 15
percentTax = liabilityTax / income
Case Is <= 85650
liabilityTax = 4867.5 + (0.25 * (income - 35350))
taxBracket = 28
percentTax = liabilityTax / income
Case Is <= 17865
liabilityTax = 17442.5 + (0.28 * (income - 85650))
taxBracket = 33
percentTax = liabilityTax / income
Case Is <= 388350
liabilityTax = 43482.5 + (0.33 * (income - 178650))
taxBracket = 35
percentTax = liabilityTax / income
Case Is > 388350
liabilityTax = 112683.5 + (0.35 * (income - 388350))
taxBracket = 38
percentTax = liabilityTax / income
End Select
Dim txtLiability As String = FormatCurrency(liabilityTax)
Dim txtBracket As String = FormatPercent(taxBracket)
Dim txtPercent As String = FormatCurrency(percentTax)```

Regards Les, Livingston, Scotland

• Edited by Saturday, September 29, 2012 7:46 PM
• Proposed as answer by Saturday, September 29, 2012 11:10 PM
• Marked as answer by Monday, October 08, 2012 1:07 PM
Saturday, September 29, 2012 7:46 PM
• Otis,

I thought that I'd toss this out as an alternate way. In mine, I use type Decimal rather than Double because you're dealing with money. I also have a few other things that I'm doing differently than yours.

Specifically, I'm using the .Validating event of the TextBox to test first that what they entered can be converted to a type Decimal and, if so, that the value is positive. If not, then I use an ErrorProvider control to display the error (there's one in your toolbox so just grab it and add it to your form - you'll see it show up in the components tray) and I then set e.Cancel to true in the .Validating event thus "locking them in" so they have to modify it to an acceptable value.

If they prefer, they can clear the TextBox entirely in order to clear the error but of course doing so will prevent them from being able to click the "Calculate" button.

The form consists of one TextBox, one Button, and four Labels. I have the code for it shown on a page of my website here and it's fairly well commented to explain what's going on.

I hope that helps. :)

Monday, October 01, 2012 1:28 PM

in the line you have in your code:

Case CDbl(income > 388350)

First the compiler evaluate the inside of the parenthesis:

(income > 388350)

Which will evaluate as TRUE or FALSE

Then it will evaluate

CDbl(True)  = -1.0

or

CDbl (False) = 0.0

Since your income has nothing to do with those, the code in the cases block is not run and you endup with 0

This is the syntax you should use

Case income > CDbl( 388350)

Monday, October 01, 2012 3:06 PM

### All replies

• Here is an example that may correct some of the issues in your code. (there are others). I changed some items instead of setting up TextBoxes etc. but you will get the idea.

```       Dim txtIncome As String = "388350.88"
Dim income As Double = CDbl(txtIncome)
Dim liabilityTax, taxBracket, percentTax As Double
Select Case income
Case Is <= 8700
liabilityTax = income * 0.1
taxBracket = 0
percentTax = liabilityTax / income
Case Is <= 35350
liabilityTax = (870 + (0.15 * (income - 8700)))
taxBracket = 15
percentTax = liabilityTax / income
Case Is <= 85650
liabilityTax = 4867.5 + (0.25 * (income - 35350))
taxBracket = 28
percentTax = liabilityTax / income
Case Is <= 17865
liabilityTax = 17442.5 + (0.28 * (income - 85650))
taxBracket = 33
percentTax = liabilityTax / income
Case Is <= 388350
liabilityTax = 43482.5 + (0.33 * (income - 178650))
taxBracket = 35
percentTax = liabilityTax / income
Case Is > 388350
liabilityTax = 112683.5 + (0.35 * (income - 388350))
taxBracket = 38
percentTax = liabilityTax / income
End Select
Dim txtLiability As String = FormatCurrency(liabilityTax)
Dim txtBracket As String = FormatPercent(taxBracket)
Dim txtPercent As String = FormatCurrency(percentTax)```

Regards Les, Livingston, Scotland

• Edited by Saturday, September 29, 2012 7:46 PM
• Proposed as answer by Saturday, September 29, 2012 11:10 PM
• Marked as answer by Monday, October 08, 2012 1:07 PM
Saturday, September 29, 2012 7:46 PM
• Otis,

I would do things a bit differently but that's not really germane.

Put a breakpoint in and follow your code line-by-line. Watch to see what's happening and you'll come away with the revelation of what's going on.

Often you'll find things in various programs that you write that just don't work quite like you thought they would - that's the norm, not the exception!

So debugging is a large part of what you should get in the habit of doing and using a breakpoint is a good start to that.

I wish you well. :)

Saturday, September 29, 2012 11:10 PM
• Otis,

I thought that I'd toss this out as an alternate way. In mine, I use type Decimal rather than Double because you're dealing with money. I also have a few other things that I'm doing differently than yours.

Specifically, I'm using the .Validating event of the TextBox to test first that what they entered can be converted to a type Decimal and, if so, that the value is positive. If not, then I use an ErrorProvider control to display the error (there's one in your toolbox so just grab it and add it to your form - you'll see it show up in the components tray) and I then set e.Cancel to true in the .Validating event thus "locking them in" so they have to modify it to an acceptable value.

If they prefer, they can clear the TextBox entirely in order to clear the error but of course doing so will prevent them from being able to click the "Calculate" button.

The form consists of one TextBox, one Button, and four Labels. I have the code for it shown on a page of my website here and it's fairly well commented to explain what's going on.

I hope that helps. :)

Monday, October 01, 2012 1:28 PM

in the line you have in your code:

Case CDbl(income > 388350)

First the compiler evaluate the inside of the parenthesis:

(income > 388350)

Which will evaluate as TRUE or FALSE

Then it will evaluate

CDbl(True)  = -1.0

or

CDbl (False) = 0.0

Since your income has nothing to do with those, the code in the cases block is not run and you endup with 0

This is the syntax you should use

Case income > CDbl( 388350)

Monday, October 01, 2012 3:06 PM
• Crazy,

My original point about using a breakpoint was so that he'd see what it was doing (and not doing) and, with that, then investigate why.

Nobody's code works exactly right every time - well I can think of one person here would say that hers does *LOL* - but anyway, I was hoping he'd figure it out on his own by running through the code using a breakpoint.

:)

Monday, October 01, 2012 3:17 PM
• I Frank, how are you doing,

I am always amazed to see how much work you do to provide a good answer. You would be a dam good teacher.

Me, I am a bit the opposite, I just put my finger where it hurt and I push.

Monday, October 01, 2012 4:15 PM
• I Frank, how are you doing,

I am always amazed to see how much work you do to provide a good answer. You would be a dam good teacher.

Me, I am a bit the opposite, I just put my finger where it hurt and I push.