944,134 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 1392
  • VB.NET RSS
Nov 3rd, 2009
0

Swapping values

Expand Post »
Hey guys here is the problem I have two variables that need their values switched , but only if the value stored in the first is less than the value stored in the second. Tried a couple of things, but am at a brick wall, I am a newbie to VB.

VB.NET Syntax (Toggle Plain Text)
  1. Public Class Form1
  2.  
  3. Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
  4. Dim getMarySales As Integer
  5. Dim getJeffSales As Integer
  6. If getMarySales < getJeffSales Then
  7. getJeffSales = txtGetMarySales.Text
  8. getMarySales = txtGetJeffSales.Text
  9. End If
  10.  
  11.  
  12.  
  13.  
  14. End Sub
Similar Threads
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Nov 3rd, 2009
0
Re: Swapping values
In line 6, you are comparing your newly created integers, which are both 0. You need to either assign them the values from the text box before the comparison, or compare the textbox values themselves (I suggest the assignment).

You should also be having a problem assigning the textbox values to the integers themselves, because textboxes not integers. You will probably want to throw a couple Int32.Parse() calls in there to convert them to integers.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 4th, 2009
0

Reply

How would I compare textbox values because if I assign values, b/c if I assign textbox values, the users couldn't enter their own, right? Please excuse my ignorance if I am wrong and please explain to me how to do that. Also I looked at the parse and see if this would be correct.

VB.NET Syntax (Toggle Plain Text)
  1. Public Class Form1
  2.  
  3. Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
  4.  
  5. Dim getMarySales As Integer = txtGetMarySales.Text
  6. Int32.Parse(getMarySales)
  7. Dim getJeffSales As Integer = txtGetJeffSales.Text
  8. Int32.Parse(getJeffSales)
  9. If getMarySales < getJeffSales Then
  10. getJeffSales = txtGetMarySales.Text
  11. getMarySales = txtGetJeffSales.Text
  12. End If
  13. End Sub
  14. End Class
Click to Expand / Collapse  Quote originally posted by mikiurban ...
In line 6, you are comparing your newly created integers, which are both 0. You need to either assign them the values from the text box before the comparison, or compare the textbox values themselves (I suggest the assignment).

You should also be having a problem assigning the textbox values to the integers themselves, because textboxes not integers. You will probably want to throw a couple Int32.Parse() calls in there to convert them to integers.
Last edited by songweaver; Nov 4th, 2009 at 1:32 pm.
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Nov 4th, 2009
0
Re: Swapping values
You don't need to set the textbox values to anything, just sompare them.

Then changes you made are almost complete, but change this
VB.NET Syntax (Toggle Plain Text)
  1. Dim getMarySales As Integer = txtGetMarySales.Text
  2. Int32.Parse(getMarySales)
to
VB.NET Syntax (Toggle Plain Text)
  1. Dim getMarySales As Integer = Int32.Parse(txtGetMarySales.Text)
(and the other one, too, as well as the ones after the comparison.)

There are several ways to write this section of code, and all will end up with the same result: your two variables holding the proper values. If you want to make your code more bulletproof, you can use Int32.TryParse() to handle problems when someone enters a non-number or decimal point in the textboxes. Also, instead of parsing the numbers twice, you can use a temp variable to help you swap the already calculated values (you will probably get an assignment later to write a Swap function like this:
VB.NET Syntax (Toggle Plain Text)
  1. Dim temp As Integer = a
  2. a = b
  3. b = temp
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 4th, 2009
0

Close

I am close getting it to work like this, however when the value jeffSales is greater than marySales it shows false not switch to the jeff value.

VB.NET Syntax (Toggle Plain Text)
  1. Public Class Form1
  2.  
  3. Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
  4.  
  5. Dim getMarySales As Integer = txtGetMarySales.Text
  6. Int32.Parse(getMarySales)
  7. Dim getJeffSales As Integer = txtGetJeffSales.Text
  8. Int32.Parse(getJeffSales)
  9. If getMarySales < getJeffSales Then
  10. txtGetMarySales.Text = txtGetJeffSales.Text AndAlso _
  11. txtGetJeffSales.Text = txtGetMarySales.Text
  12. End If
  13. End Sub
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Nov 4th, 2009
0
Re: Swapping values
In your If block, you are swapping the values of text boxes, but not the values of the variables. Did you just want the values of the textboxes swapped?
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 4th, 2009
0
Re: Swapping values
I guess that would be correct, yes because the values showing up in the text boxes need to be switched.
Click to Expand / Collapse  Quote originally posted by mikiurban ...
In your If block, you are swapping the values of text boxes, but not the values of the variables. Did you just want the values of the textboxes swapped?
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Nov 4th, 2009
0

Reply

Any help with how to make them switch without it showing false in 1st textbox?
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Visual Basic Databases
Next Thread in VB.NET Forum Timeline: playbook application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC