If statements

Going crazy but trying

Page 1 of 1

14 Replies - 8613 Views - Last Post: 06 December 2007 - 04:38 AM Rate Topic: -----

#1 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Dim Colorado As Double

'assign input to variable
Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)

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

Is This A Good Question/Topic? 0
  • +

Replies To: If statements

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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
StateName = "Colorado"
'assign input to variable
Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)

If StateName = "Colorado" Then
'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! :)
Was This Post Helpful? 0
  • +
  • -

#3 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Journey  Icon User is offline

  • New D.I.C Head

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

Re: If statements

Posted 04 December 2005 - 08:38 PM

Amadeus,

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 = "Colorado"
StateName = ","
'assign input to variable
Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)

If StateName = "Colorado" Then
'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
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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)?
Was This Post Helpful? 0
  • +
  • -

#6 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!)
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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
'assign input to variable
Totalsales = Double.Parse(Me.uiTotalSalesTextBox.Text)
StateName = Me.StateNameInputBox.Text
If StateName = "Colorado" Then
'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.
Was This Post Helpful? 0
  • +
  • -

#9 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 Journey  Icon User is offline

  • New D.I.C Head

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

Re: If statements

Posted 04 December 2005 - 09:26 PM

Amadeus,

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
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!

Thanks loads,

Journey
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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! :)
Was This Post Helpful? 0
  • +
  • -

#14 Xenon  Icon User is offline

  • Connection failed
  • member icon

Reputation: 8
  • View blog
  • Posts: 1,595
  • Joined: 12-September 05

Re: If statements

Posted 05 December 2005 - 07:52 AM

Wow, nice long thread :)
Was This Post Helpful? 0
  • +
  • -

#15 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1