|
-
Apr 14th, 2007, 06:00 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Simple Que:
What is the problem of my code ... sometimes correct but sometimes not
Code:
Dim type(2)
Dim ctr As Integer
For ctr = 0 To 2
type(ctr) = InputBox((ctr + 1) & " : " & " Enter 3 Numbers")
Next
'First Input
If type(0) > type(1) And type(2) Then
lblH.Text = type(0)
ElseIf type(0) < type(1) And type(2) Then
lblL.Text = type(0)
Else
lblM.Text = type(0)
End If
'Second Input
If type(1) > type(0) And type(2) Then
lblH.Text = type(0)
ElseIf type(1) < type(0) And type(2) Then
lblL.Text = type(1)
Else
lblM.Text = type(1)
End If
'Third Input
If type(2) > type(0) And type(1) Then
lblH.Text = type(0)
ElseIf type(2) < type(0) And type(1) Then
lblL.Text = type(2)
Else
lblM.Text = type(2)
End If
-
Apr 14th, 2007, 06:16 AM
#2
Re: Simple Que:
You should declare your type array AS something, possibly an integer since you want to populate it with numbers. Use Integer.TryParse on the InputBox to make sure the input is valid:
VB Code:
Dim type(2) As Integer
Dim ctr As Integer
For ctr = 0 To 2
Dim valid As Boolean = False
Do Until valid
valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
Loop
Next
On this line, you are treating type(2) as if it was a boolean
VB Code:
If type(0) > type(1) And type(2) Then
I suppose you want to check if type(0) is bigger than both type(1) and type(2), so youd have to do like this:
VB Code:
If type(0) > type(1) And type(0) > type(2) Then
You will have to fix that on the other If statements aswell.
And also, put Option Strict On at the very top of your code.
-
Apr 14th, 2007, 06:27 AM
#3
Thread Starter
Fanatic Member
Re: Simple Que:
When i turn on the "Option Strict" ... disallow Boolean to string .. lots of error showned up
-
Apr 14th, 2007, 06:29 AM
#4
Re: Simple Que:
Fix those errors and I can assure you that you wont be experiencing as much problems.
-
Apr 14th, 2007, 06:34 AM
#5
Thread Starter
Fanatic Member
Re: Simple Que:
I change it to this
Code:
Option Strict On
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim type(2) As Integer
Dim ctr As Integer
Dim valid As Boolean = False
Do Until valid
For ctr = 0 To 2
valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
Next
Loop
'First Input
If CBool(CInt(type(0) > type(1)) And type(0) And type(2)) Then
lblH.Text = CStr(type(0))
ElseIf CBool(CInt(type(0) < type(1)) And type(0) And type(2)) Then
lblL.Text = CStr(type(0))
Else
lblM.Text = CStr(type(0))
End If
'Second Input
If CBool(CInt(type(1) > type(0)) And type(1) And type(2)) Then
lblH.Text = CStr(type(0))
ElseIf CBool(CInt(type(1) < type(0)) And type(1) And type(2)) Then
lblL.Text = CStr(type(1))
Else
lblM.Text = CStr(type(1))
End If
'Third Input
If CBool(CInt(type(2) > type(0)) And type(2) And type(1)) Then
lblH.Text = CStr(type(0))
ElseIf CBool(CInt(type(2) < type(0)) And type(2) And type(1)) Then
lblL.Text = CStr(type(2))
Else
lblM.Text = CStr(type(2))
End If
End Sub
but why my inputbox prompt only once ?
i follow the error option
Last edited by Loraine; Apr 14th, 2007 at 06:50 AM.
Reason: Wrong Code Posted [Sorry]
-
Apr 14th, 2007, 06:37 AM
#6
Re: Simple Que:
The reason that you should have Option Strict turned on is because it catches potential issues at compile time so they don't crash your app at run time. What's worse, some issues may not crash your app but let it continue to function with incorrect data, which is what your app was doing. It may seem like more work but it will make you a better developer. The fact that you don't understand the errors it shows up is a perfect example of why it should be On: if you don't understand the errors then how can you know if there are any?
-
Apr 14th, 2007, 06:41 AM
#7
Re: Simple Que:
 Originally Posted by Loraine
I change it to this
Code:
Option Strict On
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim type(2) As Integer
Dim ctr As Integer
Dim valid As Boolean = False
For ctr = 0 To 2
Do Until valid
valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
Loop
Next
If CBool(CInt(type(0) > type(1)) And type(0) And type(2)) Then
lblH.Text = CStr(type(0))
lblM.Text = CStr(type(1))
lblL.Text = CStr(type(2))
Else
lblH.Text = CStr(type(1))
lblM.Text = CStr(type(2))
lblL.Text = CStr(type(0))
End If
If CBool(CInt(type(1) > type(0)) And type(1) And type(2)) Then
lblH.Text = CStr(type(1))
lblM.Text = CStr(type(0))
lblL.Text = CStr(type(2))
Else
lblH.Text = CStr(type(0))
lblM.Text = CStr(type(2))
lblL.Text = CStr(type(1))
End If
If CBool(CInt(type(2) > type(0)) And type(2) And type(1)) Then
lblH.Text = CStr(type(2))
lblM.Text = CStr(type(0))
lblL.Text = CStr(type(1))
Else
lblH.Text = CStr(type(0))
lblM.Text = CStr(type(1))
lblL.Text = CStr(type(2))
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lblH.Text = "0"
lblM.Text = "0"
lblL.Text = "0"
End Sub
End Class
but why my inputbox prompt only once ?
Oh sorry forgot a line there, the For-loop should look like this:
VB Code:
For ctr = 0 To 2
valid = False
Do Until valid
valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
Loop
Next
Did you take my advice on the If statements? You dont need to convert to Integer nor to Boolean.
VB Code:
type(2) > type(0) And type(2) And type(1)
What do you expect to achieve by typing And type(2) And type(1)?
-
Apr 14th, 2007, 06:55 AM
#8
Thread Starter
Fanatic Member
Re: Simple Que:
Yah i follow your advice i change it with
"If type(0) > type(1) And type(0) And type(2)"
and to my other if/s
im trying to make a "Get the highest lowest and middle numbers"
-
Apr 14th, 2007, 06:57 AM
#9
Thread Starter
Fanatic Member
Re: Simple Que:
sorry if i post multily ...
i need to convert it to integer and boolean coz its an error if i erase the "CBool and CStr"
-
Apr 14th, 2007, 07:03 AM
#10
Thread Starter
Fanatic Member
Re: Simple Que:
but if tur it to "CBool" and "CStr" i only get "1 and 0"
-
Apr 14th, 2007, 07:08 AM
#11
Re: Simple Que:
 Originally Posted by Loraine
Yah i follow your advice i change it with
"If type(0) > type(1) And type(0) And type(2)"
and to my other if/s
im trying to make a "Get the highest lowest and middle numbers"
If you look at my post again you'll see that thats not what I adviced you to write. Look again
You are getting an error because you are trying to evaulate an Integer as if it was a Boolean, if you fix your If statements the way I told you, there wont be any errors like that.
-
Apr 14th, 2007, 07:16 AM
#12
Thread Starter
Fanatic Member
Re: Simple Que:
now the output is wrong i inputed 7,254,5
Highest Num: 7
Mid Num: 254
Lowest Num: 5
Code:
Option Strict On
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim type(2) As Integer
Dim ctr As Integer
Dim valid As Boolean
Do Until valid
valid = False
For ctr = 0 To 2
valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
Next
Loop
'First Input
If type(0) > type(1) And type(0) > type(2) Then
lblH.Text = type(0).ToString
ElseIf type(0) < type(1) And type(0) < type(2) Then
lblL.Text = type(0).ToString
Else
lblM.Text = type(0).ToString
End If
'Second Input
If type(1) > type(0) And type(1) > type(2) Then
lblH.Text = type(0).ToString
ElseIf type(1) < type(0) And type(1) < type(2) Then
lblL.Text = type(1).ToString
Else
lblM.Text = type(1).ToString
End If
'Third Input
If type(2) > type(0) And type(2) > type(1) Then
lblH.Text = type(0).ToString
ElseIf type(2) < type(0) And type(2) < type(1) Then
lblL.Text = type(2).ToString
Else
lblM.Text = type(2).ToString
End If
End Sub
when i inputed 1,2,3 its fine
lblHighest.Text = 3
lblMiddle.Text = 2
lblLowest.Text = 1
but when i inputer 2,3,1 not correct
same with 3,1,2 not correct
Last edited by Loraine; Apr 14th, 2007 at 07:56 AM.
-
Apr 14th, 2007, 07:19 AM
#13
Re: Simple Que:
 Originally Posted by Loraine
It running ok now ... btw why this is wrong
(
Why is what wrong?
-
Apr 14th, 2007, 07:26 AM
#14
Thread Starter
Fanatic Member
Re: Simple Que:
This what i use to :
In my first "If statement" i'll check, if type(0) is greater that type(1) and type(2) ... if it is "TRUE" then, it will place to "lblHighest.Text"
and my "Elseif statement" if it is lower than type(1) and type(2) it will place to "lblLowest.Text" and my else if both "If and Elseif" = False then it will place to "lblMid.Text" ..... same with testing the "type(1)" and testing "type(2) .... is my algorithm wrong
-
Apr 14th, 2007, 07:39 AM
#15
Re: Simple Que:
If you want to check if type(0) is greater than type(1) and type(2), this will not work:
VB Code:
If type(0) > type(1) And type(2) Then
You have to write it like this:
VB Code:
If type(0) > type(1) And type(0) > type(2) Then
-
Apr 14th, 2007, 07:47 AM
#16
Thread Starter
Fanatic Member
Re: Simple Que:
yes i did that "could you please look post [#12]" ....
-
Apr 14th, 2007, 08:16 AM
#17
Thread Starter
Fanatic Member
Re: Simple Que:
I finished it i changed the algorithm ^^ .....
Code:
Option Explicit On
Option Strict On
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim type(2) As Integer
Dim ctr As Integer
Dim valid As Boolean
Do Until valid
valid = False
For ctr = 0 To 2
valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
Next
Loop
If type(0) > type(1) And type(0) > type(2) Then
lblH.Text = type(0).ToString
ElseIf type(0) < type(1) And type(0) < type(2) Then
lblL.Text = type(0).ToString
Else
lblM.Text = type(0).ToString
End If
If type(1) > type(0) And type(1) > type(2) Then
lblH.Text = type(1).ToString
ElseIf type(1) < type(0) And type(1) < type(2) Then
lblL.Text = type(1).ToString
Else
lblM.Text = type(1).ToString
End If
If type(2) > type(0) And type(2) > type(1) Then
lblH.Text = type(2).ToString
ElseIf type(2) < type(0) And type(2) < type(1) Then
lblL.Text = type(2).ToString
Else
lblM.Text = type(2).ToString
End If
End Sub
Thanks to you Mr. Athiest and Mr. jmCilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|