Results 1 to 17 of 17

Thread: [RESOLVED] Simple Que:

  1. #1

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Resolved [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Dim type(2) As Integer
    2.         Dim ctr As Integer
    3.         For ctr = 0 To 2
    4.             Dim valid As Boolean = False
    5.             Do Until valid
    6.                 valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
    7.             Loop
    8.         Next

    On this line, you are treating type(2) as if it was a boolean
    VB Code:
    1. 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:
    1. 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: Simple Que:

    When i turn on the "Option Strict" ... disallow Boolean to string .. lots of error showned up

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Que:

    Fix those errors and I can assure you that you wont be experiencing as much problems.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    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]

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Que:

    Quote 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:
    1. For ctr = 0 To 2
    2.             valid = False
    3.             Do Until valid
    4.                 valid = Integer.TryParse(InputBox("Enter number " & (ctr + 1).ToString & " of 3:"), type(ctr))
    5.             Loop
    6.         Next
    Did you take my advice on the If statements? You dont need to convert to Integer nor to Boolean.
    VB Code:
    1. type(2) > type(0) And type(2) And type(1)
    What do you expect to achieve by typing And type(2) And type(1)?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    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"

  9. #9

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    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"

  10. #10

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: Simple Que:

    but if tur it to "CBool" and "CStr" i only get "1 and 0"

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Que:

    Quote 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    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.

  13. #13
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Que:

    Quote Originally Posted by Loraine
    It running ok now ... btw why this is wrong

    (
    Why is what wrong?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    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

  15. #15
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. If type(0) > type(1) And type(2) Then
    You have to write it like this:
    VB Code:
    1. If type(0) > type(1) And type(0) > type(2) Then
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  16. #16

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: Simple Que:

    yes i did that "could you please look post [#12]" ....

  17. #17

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    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
  •  



Click Here to Expand Forum to Full Width