# If statements

Page 1 of 1

## 14 Replies - 9097 Views - Last Post: 06 December 2007 - 04:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=13582&amp;s=06bb71e95848c574e2add797df0955d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

# If statements

Posted 04 December 2005 - 07:56 PM

Here is my dilema, I'm supposed to create a code that will calculate the state sales tax of Colorado at 3% and all other states at 4%. Now I am half way done and decided to run it, see how I was doing .. well it's calculating but not at 3%, not sure what figure it is using. I'm also not sure I have the right variables in place.

All I know is I am learning to loathe programming with a passion.

HELP!! Below is my code thus far.

Private Sub uiCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCalcButton.Click
'declare variables
Dim Totalsales As Double
Dim Statetax As Double
Dim StateName As Double

Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)

'calculate and display sales tax
End If

Is This A Good Question/Topic? 0

## Replies To: If statements

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: If statements

Posted 04 December 2005 - 08:04 PM

You have declated all variables as doubles...this could work, but I'm not sure it's what you want...I assume that you wish to check and see if the StateName is equal to Colorado...to do so, that variable will have to be declared as a string...a string holds text...a double is a number. Also, when you are comparing a value to a text literal, you'll have t use quotes for the text...unquoted text will be interpreted as a variable name (if declared). Finally, you were calculating with the tax twice...first on the calculation, then the display. Try the following:
``` Private Sub uiCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCalcButton.Click
'declare variables
Dim Totalsales As Double
Dim Statetax As Double
Dim StateName As String
Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)

'calculate and display sales tax
Totalsales = Totalsales * 0.03
Me.uiStateTaxTextBox.Text = Totalsales
End If
End Sub

```

That should give a rudimentary example of how you want the program to work...it can be altered as required to fit your parameters.

Loathing programing eh? Well, don't give up...like everything, it requires practice!

### #3 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

## Re: If statements

Posted 04 December 2005 - 08:13 PM

Okay I see the difference between your code and mine and where I made a couple of major boo' boo's. However, loathing programming is becomes easier as each week of this course continues. When does it kick in and become fun rather than a chore?

Armadeus, thank you. Now I will just have to do some repetition to give me 4% sales tax for all other statements.

Odd thing is, I had no trouble using "IF" statements in Access, you would think they would be similar but they aren't.

Thank you for helping an old woman **impish grin** I may retain the blonde and not go gray yet for another couple of hours! LOL

### #4 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

## Re: If statements

Posted 04 December 2005 - 08:38 PM

You are a lamb. I finally got the whole code to work. Using your corrections I was able to fulfil the entire project so that no matter what state you put in, only Colorado was computed at the 3% sales Tax rate.

Below is what has been created. Again I thank you

Private Sub uiCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCalcButton.Click
'declare variables
Dim Totalsales As Double
Dim Statetax As Double
Dim StateName As String
StateName = ","
Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)

'calculate and display sales tax
Totalsales = Totalsales * 0.03
Me.uiStateTaxTextBox.Text = Totalsales
End If

If StateName = "," Then
'calculate and display sales tax
Totalsales = Totalsales * 0.04
Me.uiStateTaxTextBox.Text = Totalsales
End If
End Sub

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: If statements

Posted 04 December 2005 - 08:43 PM

Well, I'm always glad to help...as an additional note, you have assigned multiple values to the StateName variable...first you have assigned it "Colorado", then ",". Only the final assignment will hold true. In your application, are you getting the state name from another source (user entering it, perhaps)?

### #6 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

## Re: If statements

Posted 04 December 2005 - 08:52 PM

Okay,

Now I'm confused. Right now I am the user entering the state name. So what you are telling me is that if I put in Colorado again, it won't give me the 3% but the 4% of the other states????

All that jumping up and down that I did ..was for not???

OY!! Does this mean I am back to the drawing board?

Journey

### #7 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

## Re: If statements

Posted 04 December 2005 - 08:56 PM

Your correct, my code is wrong .. I'm getting 4% no matter what state I enter. Grrrrrr .... Now I have to figure out how to get multiple strings to work .. this is getting more complicated than I think I'm capable of understanding. (I never thought I would hear myself say that either in my life!)

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: If statements

Posted 04 December 2005 - 09:14 PM

Well, I'm assuming that you are entering the state name in the same manner that you are entering the TotalSales amount...is that correct? Do you have a box on the form in wich you are entering the name of the state? If so, and for the sake of argument let us assume the box is named StateNameInputBox, you can modify your code to look like this:
```Private Sub uiCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCalcButton.Click
'declare variables
Dim Totalsales As Double
Dim Statetax As Double
Dim StateName As String
Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)
StateName = Me.StateNameInputBox.Text
'calculate and display sales tax
Totalsales = Totalsales * 0.03
Me.uiStateTaxTextBox.Text = Totalsales
Else
'calculate and display sales tax
Totalsales = Totalsales * 0.04
Me.uiStateTaxTextBox.Text = Totalsales
End If
End Sub

```

If you are entering the State name in another fashion, please let me know what it is, and we can alter accordingly.

### #9 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

## Re: If statements

Posted 04 December 2005 - 09:20 PM

I have a box where I am entering random numbers for total sales, and another box where I am entering random states. The only state I have to be certain is being calculated at 3% sales tax is Colorado. All other states and country's or whatever I enter is calculated at 4%.

So the form is pretty simplistic.

### #10 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

## Re: If statements

Posted 04 December 2005 - 09:26 PM

The second code you used works no matter what I put in. Now I do have a question, what is the difference between the "Else" and the "Not" or the "Or" statements between the "IF" statements? Why can't you have an "End If" at the end of each one, or do they require to be connected?

Journey

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: If statements

Posted 04 December 2005 - 10:16 PM

They do not require connection...it is merely more efficient from a programming perspective. You can use separated if statements, but if you were to do it that way, the variable would have to be tested twice...once for each statement. Using an if else structure allows the test to be perfomed only once, especially since you are only looking for one particular state...Colorado. By doing it that way, the variable is tested to see if it contains Colorado...if it does, the 3 percent is used. Since you are only doing that for Colorado, it does not matter what elese is entered if it's not Colorado...whatever is there, the 4 percent is used.

Using a Not statement implies a boolean test...the condition supplied will be tested to see if it returns a true or false. You could have phrased that statement as if the value of state name is not equal to Colorado, perform an action.

Using an OR, or AND, allows to test that multiple conditions have been met...for example
```If StateName = "Colorado" OR StateName = "California"
Totalsales = Totalsales * 0.03
Me.uiStateTaxTextBox.Text = Totalsales
Else
Totalsales = Totalsales * 0.04
Me.uiStateTaxTextBox.Text = Totalsales
End If

```

would allow the three percent sales tax to be applied if either Colorado or California were entered by the user.

### #12 Journey

Reputation: 0
• Posts: 16
• Joined: 27-November 05

## Re: If statements

Posted 04 December 2005 - 11:06 PM

No offense to the Professor who teaches VB, but your explanation does make it easier to understand. The book is ambiguous at best. Do they have a VB.net for Dummies?

I'm afraid you may be seeing more of me than you want too over the next 9 weeks.

I do wonder if I will ever get it though. Internet Security was a breeze compared to this!

Journey

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: If statements

Posted 05 December 2005 - 07:19 AM

Glad to be of help, and please feel free to post any questions you have!

### #14 Xenon

• Connection failed

Reputation: 9
• Posts: 1,595
• Joined: 12-September 05

## Re: If statements

Posted 05 December 2005 - 07:52 AM

### #15 ferrari12508

• D.I.C Lover

Reputation: 3
• Posts: 1,115
• Joined: 02-November 07

## Re: If statements

Posted 06 December 2007 - 04:38 AM

1 Make statename a string
```dim statename as string

```

2 you have to assign it to a textbox or something
```statename = textbox1.text

```

Then you need to set totalsales = totalsales * 3
```totalsales = totalsales * 3

```

finally
```Me.uiStateTaxTextBox.Text = Totalsales.tostring

```

This post has been edited by ferrari12508: 06 December 2007 - 04:41 AM