Tryparse Statement

Assigning amount from a combo box

Page 1 of 1

4 Replies - 9901 Views - Last Post: 25 June 2008 - 06:27 PM Rate Topic: -----

#1 neongabby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 08

Tryparse Statement

Post icon  Posted 25 June 2008 - 09:03 AM

I am working on a salary review project. In the combo's selectedindexchanged event, I need to assign the raise amount selected from the combo boxes to the raise variables. I can't get the Tryparse statement correct. Here is the code I have so far:
Public Class mainForm
	Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'add raise amounts to combo boxes
		For raise As Integer = 1 To 10 Step 1
			ComboBox1.Items.Add(raise.ToString)
		Next
		For raise As Integer = 1 To 10 Step 1
			ComboBox2.Items.Add(raise.ToString)
		Next
		For raise As Integer = 1 To 10 Step 1
			ComboBox3.Items.Add(raise.ToString)
		Next

		'display current salaries
		Call ShowCurrentSalary()

	End Sub
	Private Function ShowProjection(ByVal salary As Decimal, _
							   ByVal raise As Decimal)

		salary = salary * (1 + (raise * 0.01))
		Return salary

	End Function
	Private Function GrantRaise(ByRef salary As Decimal, _
						ByRef raise As Decimal)

		salary = salary * (1 + (raise * 0.01))
		Return salary
	End Function
	Private Sub ShowCurrentSalary()
		Dim currentsalary1 As Decimal = 10000
		Dim currentsalary2 As Decimal = 11500
		Dim currentsalary3 As Decimal = 12300

		current1Label.Text = currentsalary1.ToString("C2")
		current2Label.Text = currentsalary2.ToString("C2")
		current3Label.Text = currentsalary3.ToString("C2")

	End Sub

	Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
		Me.Close()
	End Sub

	Private Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
		projected1Label.Text = String.Empty
		projected2Label.Text = String.Empty
		projected3Label.Text = String.Empty
	End Sub

	Private Sub projectedButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles projectedButton.Click
		Dim salary As Decimal
		Dim raise As Integer

		Call ShowProjection(salary, raise)

		projected1Label.Text = salary.ToString("C2")
		projected2Label.Text = salary.ToString("C2")
		projected3Label.Text = salary.ToString("C2")

		Call ShowCurrentSalary()

	End Sub

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Dim raise As Integer
		Dim salary As Decimal

		'assign value of selected raise
		raise = Integer.TryParse(projected1Label.Text, salary)

	End Sub

	Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

	End Sub

	Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged

	End Sub

	Private Sub grantButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grantButton.Click
		Dim salary As Decimal
		Dim raise As Integer

		Call ShowProjection(salary, raise)

		current1Label.Text = salary.ToString("C2")
		current2Label.Text = salary.ToString("C2")
		current3Label.Text = salary.ToString("C2")

		Call ShowCurrentSalary()
	End Sub
End Class



I'm sure I'm using wrong variables or something. Any guidance is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Tryparse Statement

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Tryparse Statement

Posted 25 June 2008 - 10:37 AM

The TryParse function returns a Boolean if the conversion is successful.

The converted value will be stored in the second parameter of the function call, currently it will be stored in salary. So if you want it stored in raise, then put it in place of salary in the Try.Parse function call.

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Dim raise As Integer
		Dim salary As Decimal
		Dim success As Boolean

		'assign value of selected raise
		success = Integer.TryParse(projected1Label.Text, salary)

	End Sub

Was This Post Helpful? 0
  • +
  • -

#3 neongabby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 08

Re: Tryparse Statement

Posted 25 June 2008 - 11:10 AM

View Postjayman9, on 25 Jun, 2008 - 10:37 AM, said:

The TryParse function returns a Boolean if the conversion is successful.

The converted value will be stored in the second parameter of the function call, currently it will be stored in salary. So if you want it stored in raise, then put it in place of salary in the Try.Parse function call.

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Dim raise As Integer
		Dim salary As Decimal
		Dim success As Boolean

		'assign value of selected raise
		success = Integer.TryParse(projected1Label.Text, salary)

	End Sub


My code must be wrong elsewhere. That didn't work. :(
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Tryparse Statement

Posted 25 June 2008 - 11:34 AM

Well you did mention, I just noticed, in your first post that you needed to get the value from the ComboBox. Currently, it looks like you are getting it from a Label.

Try the following.
Integer.TryParse(ComboBox1.SelectedItem.ToString(), salary)

Was This Post Helpful? 0
  • +
  • -

#5 neongabby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-June 08

Re: Tryparse Statement

Posted 25 June 2008 - 06:27 PM

View Postjayman9, on 25 Jun, 2008 - 11:34 AM, said:

Well you did mention, I just noticed, in your first post that you needed to get the value from the ComboBox. Currently, it looks like you are getting it from a Label.

Try the following.
Integer.TryParse(ComboBox1.SelectedItem.ToString(), salary)


Thank you. It's a stubborn little fella. I'm going to start over and see what happens.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1