| | |
Swapping values
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 47
Reputation:
Solved Threads: 0
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)
Public Class Form1 Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click Dim getMarySales As Integer Dim getJeffSales As Integer If getMarySales < getJeffSales Then getJeffSales = txtGetMarySales.Text getMarySales = txtGetJeffSales.Text End If End Sub
•
•
Join Date: Oct 2009
Posts: 62
Reputation:
Solved Threads: 15
0
#2 21 Days Ago
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.
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.
•
•
Join Date: Mar 2009
Posts: 47
Reputation:
Solved Threads: 0
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)
Public Class Form1 Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click Dim getMarySales As Integer = txtGetMarySales.Text Int32.Parse(getMarySales) Dim getJeffSales As Integer = txtGetJeffSales.Text Int32.Parse(getJeffSales) If getMarySales < getJeffSales Then getJeffSales = txtGetMarySales.Text getMarySales = txtGetJeffSales.Text End If End Sub End Class
•
•
•
•
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; 21 Days Ago at 1:32 pm.
•
•
Join Date: Oct 2009
Posts: 62
Reputation:
Solved Threads: 15
0
#4 21 Days Ago
You don't need to set the textbox values to anything, just sompare them.
Then changes you made are almost complete, but change this to (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:
Then changes you made are almost complete, but change this
VB.NET Syntax (Toggle Plain Text)
Dim getMarySales As Integer = txtGetMarySales.Text Int32.Parse(getMarySales)
VB.NET Syntax (Toggle Plain Text)
Dim getMarySales As Integer = Int32.Parse(txtGetMarySales.Text)
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)
Dim temp As Integer = a a = b b = temp
•
•
Join Date: Mar 2009
Posts: 47
Reputation:
Solved Threads: 0
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)
Public Class Form1 Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click Dim getMarySales As Integer = txtGetMarySales.Text Int32.Parse(getMarySales) Dim getJeffSales As Integer = txtGetJeffSales.Text Int32.Parse(getJeffSales) If getMarySales < getJeffSales Then txtGetMarySales.Text = txtGetJeffSales.Text AndAlso _ txtGetJeffSales.Text = txtGetMarySales.Text End If End Sub
![]() |
Similar Threads
- No Clue (C++)
- Maybe too late but as the saying goes... (C)
- Void Array Sorting/Swapping (C++)
- Drawing a Demi-Death Star: Questions About Bresenham's Line Algorithm in C++ (C++)
- Image Swapping (JavaScript / DHTML / AJAX)
- Swapping concept in c++ (C++)
- Swapping Without Third Variable (C)
- swapping with pointers ... (C)
Other Threads in the VB.NET Forum
- Previous Thread: Visual Basic Databases
- Next Thread: Child Form not Centering
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account arithmetic array basic beginner bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter forms ftp generatetags google hardcopy html images input insert intel internet mobile monitor net networking objects open output panel passingparameters pdf picturebox picturebox1 port position print printing problem save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





