Hey guys, sorry for the "Homework help" thread. I'm learning Visual Basic. I'll delete this thread as soon as I'm answered. I have to incorporate a TryParse method into a beginner program. I'm not exactly sure how to do that, this is my shitty code.
If any of you can help, I'd very much appreciate it:
Dim intNumberA As Integer ' To hold value in box A.
Dim intNumberB As Integer ' To hold value in box B.
' Clear any previously displayed message.
lblmessage.Text = String.Empty
' Retain the values from the textboxes.
intNumberA = CDbl(TextBoxA.Text)
intNumberB = CDbl(TextBoxB.Text)
'Compare the values
If intNumberA > intNumberB Then
lblmessage.Text = "Value A is greatest!"
Else
If intNumberA < intNumberB Then
lblmessage.Text = "Value B is greatest!"
Else
If intNumberA = intNumberB Then
lblmessage.Text = "Both values are equal!"
End If
End If
End If
>>55744133
Replace CDbl with Double.TryParse?
>>55744133
>>55744195
Okay, here you go, faggot. I don't know if it actually compiles or not. I did in a notepad.Dim intNumberA As Integer ' To hold value in box A.
Dim intNumberB As Integer ' To hold value in box B.
' Clear any previously displayed message.
lblmessage.Text = String.Empty
' Retain the values from the textboxes.
Dim result As Boolean = Int32.TryParse(TextBoxA.Text, out intNumberA) && Int32.TryParse(TextBoxB.Text, out intNumberB)
'Compare the values
If (result) Then
If intNumberA > intNumberB Then
lblmessage.Text = "Value A is greatest!"
Else If intNumberA < intNumberB Then
lblmessage.Text = "Value B is greatest!"
Else If intNumberA = intNumberB Then
lblmessage.Text = "Both values are equal!"
End If
Else
lblmessage.Text = "Error!"
End if
>>55744254
Replace "&&" with "And" though.
>>55744254
Thanks, I had to change some things as I was getting syntax errors, but it works now. Thank you dude, would buy you a beer.Dim intNumberA As Integer ' To hold value in box A.
Dim intNumberB As Integer ' To hold value in box B.
' Clear any previously displayed message.
lblmessage.Text = String.Empty
' Retain the values from the textboxes.
intNumberA = CDbl(TextBoxA.Text)
intNumberB = CDbl(TextBoxB.Text)
' Get the values.
If Integer.TryParse(TextBoxA.Text, intNumberA) Then
Integer.TryParse(TextBoxB.Text, intNumberB)
'Compare the values
If intNumberA > intNumberB Then
lblmessage.Text = "Value A is greatest!"
Else
If intNumberA < intNumberB Then
lblmessage.Text = "Value B is greatest!"
Else
If intNumberA = intNumberB Then
lblmessage.Text = "Both values are equal!"
End If
End If
End If
End If
It's providing a real bitch to figure out how to avoid having the program break when letters are entered in the boxes.
>>55744133
>>55744411
Put this line inside a conditionalInteger.TryParse(TextBoxB.Text, intNumberB)
Like this:If (Integer.TryParse(TextBoxA.Text, intNumberA) And Integer.TryParse(TextBoxB.Text, intNumberB)) Then
Otherwise If your second number is not a proper number it's not gonna work properly, because you're not checking for that.
>>55744459
Thanks.
>>55744446
I'm sure people mind at this time midnight when /g/ is dead. Deleting in five minutes though. Thanks, I'm going to start lurking this board.
>>55744446
This doesn't fall under tech support, just so you know.
Homework?
You only know it's my homework as I told you it is, many people do this on their own personal time. I'm trying to pick up a skill, this is week 1, cut me some slack.