Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello, i am hoping someone can help.

I currently have 3 text boxes which are fetching data from a webclient. The first textbox displays a number, the second displays the same source after 1 second to give a differnt number. The third box then takes one from the other to give a figure per second. (below as textbox8)

The problem i am having is that number fluctuates and i need to store the highest value it reaches in a fourth textbox (Below as textbox9)

So if textbox9 is recording the highest number generated in textbox 8 and actual number in textbox8 is lower than previously recorded it skips it, otherwise it will record it.

I have tried with the following code:
VB
If TextBox9.Text > TextBox8.Text Then
            GoTo bob
        Else : TextBox9.Text = TextBox8.Text
        End If
Bob:


But it is only reading the first number of the whole number so to speak.
Example if textbox9 reads 999 and textbox8 is 1000 it will not change.
It seems to take the first number (i.e. "1") from the 1000 and decides it is smaller than the first "9" from 999.
Can anyone help me to show that "1000" is bigger than "999"?

This is also the case with "9" as the first number in the number range of 10-89 is deemed to be smaller than "9" as it only reads the first number of the whole number. (ie the 8 from 89 is smaller than 9). Apart from that it does work in part where it can tell 44 is bigger than 31. Again it reads the first number.

Anyone got a clue?

Thanks In Advance.

Oh and the number is not a decimal (its a whole number). The output is a string and the numbers add/subtract ok.

Thanks again
Posted

This bit of code that you have:
TextBox9.Text > TextBox8.Text

is comparing string values. It doesn't know that you are comparing numbers, so it takes "1000" and it knows that "1" comes before "9" so it thinks that "1000" is less than "999". What you want to do is first convert the text into a number and then do the compare. Personally, I like to use my own conversion function:
VB
Public Function CvtStrDec(ByVal str As String) As Decimal
        If str Is Nothing Then Return 0
        If str Is DBNull.Value Then Return 0
        If str.Trim.Length = 0 Then Return 0
        If Not IsNumeric(str) Then Return 0
        Return CDec(str)
End Function


And then you'd use it like this:

cvtStrDec(TextBox9.Text) < cvtStrDec(TextBox8.Text)


That will work for any number, but if you really need it as an integer you can do something similar with this:
VB
Public Function CvtStrInt(ByVal str As String) As Integer
       If str Is Nothing Then Return 0
       If str Is DBNull.Value Then Return 0
       If str.Trim.Length = 0 Then Return 0
       If Not IsNumeric(str) Then Return 0
       Return CInt(str)
End Function


Also, it's a good idea to get into the habit of renaming your textboxes to more meaningful names. You might want to look into some good naming convention practices.
 
Share this answer
 
If you want a string comparison, compare strings. If you want any other form of comarison, compare other objects.

In your case, you want to compare numbers, specifically integers, so compare numbers not strings.
"1000" is less than "999" because '1' is before '9' in the character set.

Instead try:
If int.Parse(TextBox9.Text) > int.Parse(TextBox8.Text) Then
            GoTo bob
        Else : TextBox9.Text = TextBox8.Text
        End If
Bob:

Then, you can do two other things:
1) Forget you ever heard about goto, and do not use it again until you have three years experience. Then you will stand a chance of knowing when it is a good idea to use it. You probably still won't come across any cases when you need to though. That is probably the worst example of how not-to-use-goto I have seen in ages. Replace your code:
If int.Parse(TextBox9.Text) <= int.Parse(TextBox8.Text) Then
        TextBox9.Text = TextBox8.Text
End If


2) Stop using the default VS names and use sensible ones instead. You may remember today which text box has the maximum value, but will you remember next week? Call it tbMaxValue instead and you don't habve to remember...
 
Share this answer
 
You are comparing strings and not numbers, you need to convert the textboxes' text into a number and only then compare them.
 
Share this answer
 
Thanks 4 the quick responce.

how do you do that?

Thanks
 
Share this answer
 
Comments
Kschuler 13-May-11 15:33pm    
When you have a question regarding someone's solution, you should click the Add Comment link in the solution, otherwise that person does not receive any notification.
originalgriff:

Thanks but code does not work as int is highlighted as does not support so many arguments. The copy and paste is from a seperate instance of my app (just trying code) and the real instance does use datalbl and resulttxt etc...
I am a self teaching novice and only used vb6 to make games ...many years ago in school and not in a lession either. Trying to get back into it.

Many thanks
 
Share this answer
 
Kschuler:
Many thanks.

Code working fine in my test app. Will no recode my real app. (running 2 instances of .net as not to corrupt my working app.

With reference to the "goto" it was only in the testing window...not in use in the application.

Thanks Again.

Richard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900