Coversion

Coversion

Page 1 of 1

9 Replies - 651 Views - Last Post: 22 October 2009 - 11:56 PM Rate Topic: -----

#1 pomak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 01-November 08

Coversion

Post icon  Posted 22 October 2009 - 12:57 PM

Hi people I have problem with this code it suppose to display the bonus amount but when I enter the code which is 1 or 2 a message box pops up and it says
that the "Sales amount must be numeric" I do not know what is wrong with the code I think I am not doing the conversion right
here is the code
thanks in advance



Option Explicit On
Option Strict On

Imports System.Globalization

Public Class MainForm

	Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
'Close the form
		Me.Close()
	End Sub
	Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcButton.Click
		' calculates a bonus amount

		Dim code As String
		Dim sales As Decimal
		Dim bonus As Decimal
		Dim isConverted As Boolean

		' convert sales amount to a number
		isConverted = Decimal.TryParse(salesTextBox.Text, NumberStyles.Currency, _
			NumberFormatInfo.CurrentInfo, sales)

		If isConverted Then
			code = codeTextBox.Text
			If code = "1" OrElse code = "2" AndAlso sales > 10000 Then
				bonus = sales * 0.1D
			Else
				bonus = sales * 0.05D
			End If
			MessageBox.Show("Your bonus is " & bonus.ToString("C2"), _
				"Bonus Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)

		Else
			MessageBox.Show("The sales amount must be numeric.", _
				   "Bonus Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)

		End If

		' set the focus
		codeTextBox.Focus()
	End Sub
End Class






Is This A Good Question/Topic? 0
  • +

Replies To: Coversion

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Coversion

Posted 22 October 2009 - 01:04 PM

What value are you entering in the salesTextBox?
Was This Post Helpful? 0
  • +
  • -

#3 pomak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 01-November 08

Re: Coversion

Posted 22 October 2009 - 01:12 PM

View PostCharlieMay, on 22 Oct, 2009 - 12:04 PM, said:

What value are you entering in the salesTextBox?


I am entering numbers 1 or 2
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Coversion

Posted 22 October 2009 - 01:22 PM

You have 2 textboxes correct?
1 for Sales amount
1 for Code
Your code is written so that it requires a value capable of being parsed as a decimal in the Sales Text Box

The Code Textbox can be anything and you will get a result but will get more specific results if it is "1" or "2"

If I enter 1000 in the Sales Box and 1 in the Code textbox I get the following result: Your Bonus Is: $100.00

The only way I see to get the other message is to enter a string in the Sales Textbox that can't be converted or leave the sales textbox blank.
Was This Post Helpful? 0
  • +
  • -

#5 pomak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 01-November 08

Re: Coversion

Posted 22 October 2009 - 01:32 PM

View PostCharlieMay, on 22 Oct, 2009 - 12:22 PM, said:

You have 2 textboxes correct?
1 for Sales amount
1 for Code
Your code is written so that it requires a value capable of being parsed as a decimal in the Sales Text Box

The Code Textbox can be anything and you will get a result but will get more specific results if it is "1" or "2"

If I enter 1000 in the Sales Box and 1 in the Code textbox I get the following result: Your Bonus Is: $100.00

The only way I see to get the other message is to enter a string in the Sales Textbox that can't be converted or leave the sales textbox blank.



yea I see

I think it is not calculating right when I type 1 in the code text box and 200 in the sales text box it shows that the bonus amount is 20 dollars which is not true it should be 10 dollars because the sales amount is under 10,000 how can I fix that
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Coversion

Posted 22 October 2009 - 01:39 PM

If I'm understanding correctly I think all you would have to do there is

If code = "1" Or sales > 10000 Then


If code is 1 or sales >10000 then bonus is 1% else bonus is .5%
Was This Post Helpful? 0
  • +
  • -

#7 pomak  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 01-November 08

Re: Coversion

Posted 22 October 2009 - 01:45 PM

View PostCharlieMay, on 22 Oct, 2009 - 12:39 PM, said:

If I'm understanding correctly I think all you would have to do there is

If code = "1" Or sales > 10000 Then


If code is 1 or sales >10000 then bonus is 1% else bonus is .5%





I tried but I still need code 2 maybe I have to do another if for 2
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Coversion

Posted 22 October 2009 - 02:26 PM

"2" should be handled by the else

If "2" it is always .5% unless the sales is > 10000 then it will be 1%
if "1" it is always 1% regardless of the sales amount

is that not what you are wanting?

The only other thing would be to make sure that either a 1 or a 2 is entered in the textbox making all other input invalid. You can do that by ether checking to see if it is = to "1" or "2" or you can put code in the keypress that will only allow those two characters to be entered.

Right now if your code is not 1 and your sales is less then 10001 it will calculate the sales at .5% even if you left the code text blank. You could even put Hello in the code box and it would calculate sales by .5%

if code <>"1" and code <>"2" then
messagebox.show("invalid entry")... something like this would handle that part for you.
Was This Post Helpful? 0
  • +
  • -

#9 CodeNo0b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-October 09

Re: Coversion

Posted 22 October 2009 - 07:14 PM

Why don't you just use a int for the code?

Also you can use val() to get the numerical value of String.


If all else fails, use try-catch to skip error & act like nothing happend.

This post has been edited by CodeNo0b: 22 October 2009 - 07:19 PM

Was This Post Helpful? 0
  • +
  • -

#10 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Coversion

Posted 22 October 2009 - 11:56 PM

Hi,

Here is your problem...

		isConverted = Decimal.TryParse(salesTextBox.Text, NumberStyles.Currency, _
			NumberFormatInfo.CurrentInfo, sales)



isConverted is a boolean type so don't try and use the TryParse. as far as i knnow, 0 = false , 1 = true , any other number = false

This line

   If isConverted Then



is equal to

	If isConverted = True Then



so do this instead...

		 if isNumeric(salesTextBox.Text) = true then
			isConverted = True
		 else
			isConverted = False
		 end If

		If isConverted Then
			code = codeTextBox.Text
			If code = "1" OrElse code = "2" AndAlso sales > 10000 Then
				bonus = sales * 0.1D
			Else
				bonus = sales * 0.05D
			End If
			MessageBox.Show("Your bonus is " & bonus.ToString("C2"), _
				"Bonus Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)

		Else
			MessageBox.Show("The sales amount must be numeric.", _
				   "Bonus Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)

		End If




you also might want to check if the "codeTextBox.Text" is numeric aswell.....


;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1