Results 1 to 32 of 32

Thread: Final attempt.... HELP what am i doing wrong?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Question Final attempt.... HELP what am i doing wrong?

    I need to write an application that allows the user to enter three Double values, then determines and
    displays the smallest and largest values.

    Using functions to get s Minimum and Maximum that each receive three Double values and return a Double result and by using the math.min math.max.

    I also need to do input verification so only numbers can be entered and not a string.

    What am I doing wrong now!!!????

    vb Code:
    1. Option Strict On
    2. Public Class HighestLowest
    3.  
    4.  
    5.     ' code pseoudo
    6.     ' INPUT 3 numbers
    7.     ' pass to function lowest
    8.     ' return lowest DISPLAY lowest
    9.     ' pass to function highest
    10.     ' return highest DISPLAY highest
    11.  
    12.     ' global vars
    13.     Dim oneNum As Double = 0
    14.     Dim twoNum As Double = 0
    15.     Dim threeNum As Double = 0
    16.     Dim highNum As Double
    17.     Dim lowNum As Double
    18.  
    19.     Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
    20.  
    21.         ' verify that the input is a number
    22.         If IsNumeric(input1Tbx.Text) = True Then
    23.             oneNum = CDbl(input1Tbx.Text)
    24.        ElseIf : MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    25.             input1Tbx.Text = ""
    26.             input2Tbx.Text = ""
    27.             input3Tbx.Text = ""
    28.             input1Tbx.Focus()
    29.             Exit Sub
    30.             If IsNumeric(input2Tbx.Text) = True Then
    31.                 twoNum = CDbl(input2Tbx.Text)
    32.             ElseIf : MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    33.                 input1Tbx.Text = ""
    34.                 input2Tbx.Text = ""
    35.                 input3Tbx.Text = ""
    36.                 input1Tbx.Focus()
    37.                 Exit Sub
    38.                 If IsNumeric(input3Tbx.Text) = True Then
    39.                     threeNum = CDbl(input3Tbx.Text)
    40.                 ElseIf : MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    41.                     input1Tbx.Text = ""
    42.                     input2Tbx.Text = ""
    43.                     input3Tbx.Text = ""
    44.                     input1Tbx.Focus()
    45.                     Exit Sub
    46.                 End If
    47.             End If
    48.         End If
    49.  
    50.  
    51.         highNum = largest(oneNum, twoNum, threeNum)
    52.         largestLbl.Text = highNum.ToString
    53.  
    54.         lowNum = smallest(oneNum, twoNum, threeNum)
    55.         smallestLbl.Text = lowNum.ToString
    56.  
    57.  
    58.     End Sub
    59.  
    60.     Function largest(ByVal one As Double, ByVal two As Double, ByVal three As Double) As Double
    61.  
    62.         Dim tempMax As Double
    63.         Dim finalMax As Double
    64.  
    65.         tempMax = Math.Max(one, two)
    66.         finalMax = Math.Max(tempMax, three)
    67.  
    68.         Return finalMax
    69.  
    70.     End Function ' Maximum
    71.  
    72.     Function smallest(ByVal one As Double, ByVal two As Double, ByVal three As Double) As Double
    73.  
    74.         Dim tempMin As Double
    75.         Dim finalMin As Double
    76.  
    77.         tempMin = Math.Min(one, two)
    78.         finalMin = Math.Min(tempMin, three)
    79.  
    80.         Return finalMin
    81.  
    82.     End Function ' Minimum
    83. End Class

    I am not getting the validation to work... where is my error??

  2. #2
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Final attempt.... HELP what am i doing wrong?

    This is obviously a homework assignment of some sort. We won't be helping you learn if we just do the code for you. With that said, we are more than willing to help.

    You tell us: Where is your error? What is the error produced? Tell us which line the error is occurring and what the error says. That will help us a lot more than you simply asking us why it's not working and where your error is.
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Final attempt.... HELP what am i doing wrong?

    Can you please stop creating multiple threads for the same issue. What you are doing wrong is either not reading the help that's provided when you ask for it or reading it and not using it. I've already told you in two other threads to use Double.TryParse and another member even provided a link to the documentation for that method, which I had already told you to read. I don't see any use of Double.TryParse in your code so you keep on asking repeatedly for help but then ignore it when it's provided. You're wasting our time that we're volunteering to help you, a complete stranger. That will become tiresome very quickly.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Final attempt.... HELP what am i doing wrong?

    this is how i'd do it. I used an errorprovider, + passed smallestLbl.Text + largestLbl.Text byref to my evaluateNumbers sub:

    Code:
    Public Class Form1
    
        Private WithEvents ep1 As New ErrorProvider
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submitBtn.Click
            Dim oneNum As Double
            Dim twoNum As Double
            Dim threeNum As Double
    
            ep1.Clear()
    
            smallestLbl.Text = ""
            largestLbl.Text = ""
    
            If Double.TryParse(input1Tbx.Text, oneNum) Then
                If Double.TryParse(input2Tbx.Text, twoNum) Then
                    If Double.TryParse(input3Tbx.Text, threeNum) Then
                        evaluateNumbers(smallestLbl.Text, largestLbl.Text, oneNum, twoNum, threeNum)
                    Else
                        ep1.SetError(input3Tbx, "Please enter numbers only.")
                    End If
                Else
                    ep1.SetError(input2Tbx, "Please enter numbers only.")
                End If
            Else
                ep1.SetError(input1Tbx, "Please enter numbers only.")
            End If
    
        End Sub
    
        Private Sub evaluateNumbers(ByRef lowVar As String, ByRef highVar As String, ByVal ParamArray numbers() As Double)
            lowVar = numbers.Min.ToString
            highVar = numbers.Max.ToString
        End Sub
    
    End Class

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    I dont even know where to start. I dont have a clue what these errors mean.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    What is the trypase, and how does that work? is it better than the CDbl?

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Final attempt.... HELP what am i doing wrong?

    So....how will asking again help?

    What you need has been stated so often by now that there isn't any point in stating it again, so let's try something different: What errors are you getting? What is the wording of the error message and on which line does the error occur?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    Found the error, needed to set the program to show the output error list... it was an error on line 51, too many end if's

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Final attempt.... HELP what am i doing wrong?

    Quote Originally Posted by VBclueless View Post
    What is the trypase, and how does that work? is it better than the CDbl?
    Another member has provided a link to the documentation for the Double.TryParse method so, if you want to know what it is and how it works, reading that documentation would be the obvious first step. Not only that, .paul. has even provided a code example that includes its use. I can only conclude from that that you are not even reading the replies that we make. If you ask for help and then don't read the replies then you're wasting our time and don't deserve our help.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Final attempt.... HELP what am i doing wrong?

    Get rid of those colons in your code and you probably won't have this problem again. Adding colons is a holdover from the dawn of BASIC, but it's a style that should be abandoned. All it ever does is cause confusion.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Cool Re: Final attempt.... HELP what am i doing wrong?

    Finally got it working.... is there a way to make the code shorter?

    Specifically to have all three error checks done at once or by a sub routine?

    vb Code:
    1. Option Strict On
    2. Public Class HighestLowest
    3.     ' Roger Mackbach CIS 212 VB Final Section A Question 1
    4.     ' Mainestreet id 0767958 and it is 5375711 ENCRPTED  30 points please.
    5.     ' code pseoudo
    6.     ' INPUT 3 numbers
    7.     ' pass to function lowest
    8.     ' return lowest DISPLAY lowest
    9.     ' pass to function highest
    10.     ' return highest DISPLAY highest
    11.  
    12.     ' global vars
    13.     Dim oneNum As Double = 0
    14.     Dim twoNum As Double = 0
    15.     Dim threeNum As Double = 0
    16.     Dim highNum As Double
    17.     Dim lowNum As Double
    18.     'Dim array1(2) As Double
    19.     'Dim count1 As Integer = 0
    20.  
    21.  
    22.     Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
    23.  
    24.         ' verify that the input is a number
    25.         If IsNumeric(input1Tbx.Text) = False Then
    26.             MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    27.             input1Tbx.Text = ""
    28.             input1Tbx.Focus()
    29.             Exit Sub
    30.         Else
    31.             oneNum = CDbl(input1Tbx.Text)
    32.             If IsNumeric(input2Tbx.Text) = False Then
    33.                 MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    34.                 input2Tbx.Text = ""
    35.                 input2Tbx.Focus()
    36.                 Exit Sub
    37.             Else
    38.                 twoNum = CDbl(input2Tbx.Text)
    39.                 If IsNumeric(input3Tbx.Text) = False Then
    40.                     MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    41.                     input3Tbx.Text = ""
    42.                     input3Tbx.Focus()
    43.                     Exit Sub
    44.                 Else
    45.                     threeNum = CDbl(input3Tbx.Text)
    46.                 End If
    47.             End If
    48.         End If
    49.  
    50.      
    51.         highNum = largest(oneNum, twoNum, threeNum)
    52.         largestLbl.Text = highNum.ToString
    53.  
    54.         lowNum = smallest(oneNum, twoNum, threeNum)
    55.         smallestLbl.Text = lowNum.ToString
    56.  
    57.  
    58.     End Sub
    59.  
    60.     Function largest(ByVal one As Double, ByVal two As Double, ByVal three As Double) As Double
    61.  
    62.         Dim tempMax As Double
    63.         Dim finalMax As Double
    64.  
    65.         tempMax = Math.Max(one, two)
    66.         finalMax = Math.Max(tempMax, three)
    67.  
    68.         Return finalMax
    69.  
    70.     End Function ' Maximum
    71.  
    72.     Function smallest(ByVal one As Double, ByVal two As Double, ByVal three As Double) As Double
    73.  
    74.         Dim tempMin As Double
    75.         Dim finalMin As Double
    76.  
    77.         tempMin = Math.Min(one, two)
    78.         finalMin = Math.Min(tempMin, three)
    79.  
    80.         Return finalMin
    81.  
    82.     End Function ' Minimum
    83. End Class

    Any ideas?

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Final attempt.... HELP what am i doing wrong?

    Yeah, you can do it much easier using Double.TryParse, but you're steadily ignoring that. In this very thread there is an example of doing the same, there's also a link to the documentation in a different thread.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    Mr. jmcilhinney,

    I am reading the replies, however, I have a learning disability, called AUTISM! I do not learn the way most people do, that is why I ask directed questions. I learn by direct example. I have great difficulty learning by substitution like you have suggested several times.

    Thank you to all that has helped me on this.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    Haggy Hiker, I am not ignoring it, I dont understand it.

    I am trying to read through it as we speak. Thanks.

  15. #15
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Final attempt.... HELP what am i doing wrong?

    I see you still aren't using the Double.TryParse. Using IsNumeric isn't the best practice and I would be willing to bet your teacher will tell you the same thing. Here's the one I built out:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim numbers(2) As Double
    
            If Double.TryParse(txtValue1.Text, numbers(0)) = False Then
                MsgBox(ShowErrorMessage(txtValue1.Text))
                txtValue1.BackColor = Color.Yellow
            End If
            If Double.TryParse(txtValue2.Text, numbers(1)) = False Then
                MsgBox(ShowErrorMessage(txtValue2.Text))
                txtValue2.BackColor = Color.Yellow
            End If
            If Double.TryParse(txtValue3.Text, numbers(2)) = False Then
                MsgBox(ShowErrorMessage(txtValue3.Text))
                txtValue3.BackColor = Color.Yellow
            End If
    
            lblSmallestValue.Text = FindLowestNumber(numbers)
            lblLargestValue.Text = FindHighestNumber(numbers)
    
        End Sub
    
        Function ShowErrorMessage(ByVal Value As String)
            Dim message As String
            message = "Your value of " + Value + " is unacceptable. Please enter numbers only"
            Return message
        End Function
    
        Public Shared Function FindHighestNumber(ByVal numbers() As Double) As Double
            Return numbers.Max
        End Function
    
        Public Shared Function FindLowestNumber(ByVal numbers() As Double) As Double
            Return numbers.Min
        End Function
    
    End Class
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Final attempt.... HELP what am i doing wrong?

    Quote Originally Posted by VBclueless View Post
    Mr. jmcilhinney,

    I am reading the replies, however, I have a learning disability, called AUTISM! I do not learn the way most people do, that is why I ask directed questions. I learn by direct example. I have great difficulty learning by substitution like you have suggested several times.

    Thank you to all that has helped me on this.
    I must have missed the part where you told us that after one of the many previous references to Double.TryParse. As for your learning by direct example, to imply that you can only possibly learn by someone else providing the code for you is just plain false, whether you have a learning disability or not. Examples are a learning aid for all of us to some degree but that's not the only way to learn. That said, if you want an example then there are plenty on the web already. Did you make any effort to try to find any? I think that it's safe to say that the answer is no, so even if you can learn only be example, you were still waiting for someone else to do for you what you could do for yourself. If you were reading the replies and still allowing us to keep on suggesting the same thing over and over without indicating that you had made any attempt to understand it makes it even worse. All I'm saying is that, if someone gives you information, do what you can to use it and let them know that you have done so and let them know if you don't understand it and why.

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Final attempt.... HELP what am i doing wrong?

    You probably should have mentioned the autism bit right from the start. That does make a difference.

    What Double.TryParse does is to do both the IsNumeric and the CDbl in a single statment. However, it also does a bit of both. Notice that IsNumeric returns True/False, but doesn't do the conversion. Meanwhile, CDbl tries to do the conversion, but it doesn't tell you whether it will work or not. Because IsNumeric doesn't do the conversion, and CDbl doesn't tell you whether or not it will work, you have to do both to get both results. Double.TryParse does both at the same time.

    Take these two lines from your code:

    25: If IsNumeric(input1Tbx.Text) = False Then

    31: oneNum = CDbl(input1Tbx.Text)

    The first line tells you whether the text can be converted, and the second line does the conversion. To use Double.TryParse, you would just do this:

    If Double.TryParse(input1Tbx.Text, oneNum) Then

    That does both of the previous two lines in a single step. Double.TryParse will return True or False the same way that IsNumeric returns True or False. Furthermore, Double.TryParse does the conversion. That's what the second argument is for. In your line 31, you were converting the text and putting it in oneNum. In the single line using Double.TryParse, if the conversion works, then the number is converted and oneNum now holds the converted number, just as you were doing with CDbl. If the conversion doesn't work, the Double.TryParse returns False, and oneNum isn't changed. It still holds whatever it held before.

    Does that help?
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Cool Re: Final attempt.... HELP what am i doing wrong?

    YES!!! thank you VERY VERY VERY much! I understood that!!!! (I am excited if you could not tell!)

    I modified my code as following, and works great! Thank you again for your time and patiance to explain this so I can understand it.

    vb Code:
    1. ' code pseoudo
    2.     ' INPUT 3 numbers
    3.     ' pass to function lowest
    4.     ' return lowest DISPLAY lowest
    5.     ' pass to function highest
    6.     ' return highest DISPLAY highest
    7.  
    8.     ' global vars
    9.     Dim oneNum As Double = 0
    10.     Dim twoNum As Double = 0
    11.     Dim threeNum As Double = 0
    12.     Dim highNum As Double
    13.     Dim lowNum As Double
    14.  
    15.     Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
    16.  
    17.         If Double.TryParse(input1Tbx.Text, oneNum) Then
    18.             If Double.TryParse(input2Tbx.Text, twoNum) Then
    19.                 If Double.TryParse(input3Tbx.Text, threeNum) Then
    20.                     'evaluateNumbers(smallestLbl.Text, largestLbl.Text, oneNum, twoNum, threeNum)
    21.                 Else
    22.                     MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    23.                     input3Tbx.Text = ""
    24.                     input3Tbx.Focus()
    25.                     Exit Sub
    26.  
    27.                 End If
    28.             Else
    29.                 MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    30.                 input2Tbx.Text = ""
    31.                 input2Tbx.Focus()
    32.                 Exit Sub
    33.  
    34.             End If
    35.         Else
    36.             MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    37.             input1Tbx.Text = ""
    38.             input1Tbx.Focus()
    39.             Exit Sub
    40.  
    41.         End If
    42.  
    43.      
    44.         highNum = largest(oneNum, twoNum, threeNum)
    45.         largestLbl.Text = highNum.ToString
    46.  
    47.         lowNum = smallest(oneNum, twoNum, threeNum)
    48.         smallestLbl.Text = lowNum.ToString
    49.  
    50.  
    51.     End Sub
    52.  
    53.     Function largest(ByVal one As Double, ByVal two As Double, ByVal three As Double) As Double
    54.  
    55.         Dim tempMax As Double
    56.         Dim finalMax As Double
    57.  
    58.         tempMax = Math.Max(one, two)
    59.         finalMax = Math.Max(tempMax, three)
    60.  
    61.         Return finalMax
    62.  
    63.     End Function ' Maximum
    64.  
    65.     Function smallest(ByVal one As Double, ByVal two As Double, ByVal three As Double) As Double
    66.  
    67.         Dim tempMin As Double
    68.         Dim finalMin As Double
    69.  
    70.         tempMin = Math.Min(one, two)
    71.         finalMin = Math.Min(tempMin, three)
    72.  
    73.         Return finalMin
    74.  
    75.     End Function ' Minimum
    76. End Class

  19. #19
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Final attempt.... HELP what am i doing wrong?

    Good deal. Glad you understand the Double.TryParse.

    Now you could look at optimizing your code if you wanted to:

    Code:
    Dim numbers(2) As Double
    That single line declares an array of three double variables.

    numbers(0)
    numbers(1)
    numbers(2)

    Check out my example a few posts above to see how I did it.
    Last edited by mbutler755; Dec 18th, 2013 at 10:41 PM. Reason: Changed a word (fat fingers)
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    Thank you mButler755. I was going to originally use an array, but I could not figure out how to index it to the function. I am looking at it right now. thank you for your help. might ask yoiu a few questions about it later after I have a min to study and pick it apart for what I don't understand.

    Thanks again.
    Last edited by VBclueless; Dec 18th, 2013 at 10:51 PM. Reason: added comment

  21. #21
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Final attempt.... HELP what am i doing wrong?

    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    Error 1 Option Strict On requires all Function, Property, and Operator declarations to have an 'As' clause. C:\Zippme\HighLowARRAY\HighLowARRAY\HighestLowestARRAY.vb 25 14 HighLowARRAY

    what does this mean?

  23. #23
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Final attempt.... HELP what am i doing wrong?

    I bet you are missing the word 'As' somewhere. Like:

    Code:
    Dim numbers(2) AS Double
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Thumbs up Re: Final attempt.... HELP what am i doing wrong?

    It was something to do with the error message box function.

    Thank you for your help.

    this is what I ended up with, and I think it is awesome... Could not have done it without everyone's help!!!

    vb Code:
    1. Option Strict On
    2. Public Class HighestLowestARRAY
    3.    
    4.     ' code pseoudo
    5.     ' INPUT 3 numbers into an array
    6.     ' pass to function lowestNumber
    7.     ' return lowest DISPLAY lowest
    8.     ' pass to function highestNumber
    9.     ' return highest DISPLAY highest
    10.     ' error message highlight box to show error and focus.
    11.  
    12.  
    13.     Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
    14.         Dim numbers(2) As Double 'set up array
    15.  
    16.         If Double.TryParse(input1Tbx.Text, numbers(0)) = False Then
    17.             input1Tbx.BackColor = Color.Yellow 'add a splash of color
    18.             MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    19.             input1Tbx.Text = "" ' clear the box
    20.             input1Tbx.Focus() ' focus the box
    21.             input1Tbx.BackColor = Color.White 'clear the color
    22.             Exit Sub
    23.  
    24.         End If
    25.         If Double.TryParse(input2Tbx.Text, numbers(1)) = False Then
    26.             input2Tbx.BackColor = Color.Yellow 'add a splash of color
    27.             MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    28.             input2Tbx.Text = "" ' clear the box
    29.             input2Tbx.Focus() ' focus the box
    30.             input2Tbx.BackColor = Color.White 'clear the color
    31.             Exit Sub
    32.  
    33.         End If
    34.         If Double.TryParse(input3Tbx.Text, numbers(2)) = False Then
    35.             input3Tbx.BackColor = Color.Yellow 'add a splash of color
    36.             MessageBox.Show("Please enter numbers only.", "Please check your input values.", MessageBoxButtons.OK, MessageBoxIcon.Error)
    37.             input3Tbx.Text = "" ' clear the box
    38.             input3Tbx.Focus() ' focus the box
    39.             input3Tbx.BackColor = Color.White 'clear the color
    40.  
    41.             Exit Sub
    42.  
    43.         End If
    44.  
    45.         smallestLbl.Text = CStr(lowestNumber(numbers)) ' send to function, pass back, then make string to display
    46.         largestLbl.Text = CStr(highestNumber(numbers)) ' send to function, pass back, then make string to display
    47.  
    48.     End Sub
    49.  
    50.     Function highestNumber(ByVal numbers() As Double) As Double
    51.         Return numbers.Max 'much cleaner than using the math.max
    52.     End Function
    53.  
    54.     Function lowestNumber(ByVal numbers() As Double) As Double
    55.         Return numbers.Min 'much cleaner than using the math.min
    56.     End Function
    57.  
    58. End Class

  25. #25
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Final attempt.... HELP what am i doing wrong?

    It looks really good. One little tip about your comments for Math.Min and Math.Max: You are still using that functionality, it's just down to a single line because you are bringing it in as a double. You may just want to change the comments to say something like:

    ' Find the highest number in the array
    ' Find the lowest number in the array
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Final attempt.... HELP what am i doing wrong?

    it's an improvement. looks a lot like Matt's example. it's good you've conquered Double.TryParse...

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    31

    Re: Final attempt.... HELP what am i doing wrong?

    Thanks paul! With out you guys help, I would still be flopping around. When I finally get good at this, I am going to volunteer my time answering question and explaining them like shaggy hiker, matt, and you! It really makes the difference when someone types an answer and explains it rather than just point to a website or worse berate you for not knowing. Thanks again for all your help!

  28. #28
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Final attempt.... HELP what am i doing wrong?

    If all those 'highestNumber' and 'lowestNumber' methods do is call Max and Min on the arrays passed in then why have those methods at all? Why not just call Max and Min on the array in the code where you're calling 'highestNumber' and 'lowestNumber'?

  29. #29
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Final attempt.... HELP what am i doing wrong?

    Quote Originally Posted by VBclueless View Post
    It really makes the difference when someone types an answer and explains it rather than just point to a website or worse berate you for not knowing.
    Noone berated you for not knowing. If you were berated it was for not trying to find out when you were told where to look. Sure, it makes a difference when someone else takes the time to explain things in great detail; you don't have to make as much effort. If you don't follow a link to a web site when someone provides it then how do you know that you don't understand the information it provides? It really comes down to taking responsibility for your own learning. We all need to ask questions at times, myself included, but if you already have resources at your disposal then you should exhaust them before asking others to provide more. It's for your own benefit because you'll learn more that way.

  30. #30
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Final attempt.... HELP what am i doing wrong?

    My little cousin has Autism. While he has learning difficulties, quite bad in his case he is still determined to make an effort on his own.

  31. #31
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Final attempt.... HELP what am i doing wrong?

    Quote Originally Posted by VBclueless View Post
    this is what I ended up with, and I think it is awesome...

    vb Code:
    1. Function highestNumber(ByVal numbers() As Double) As Double
    2.     Return numbers.Max 'much cleaner than using the math.max
    3. End Function
    4.  
    5. Function lowestNumber(ByVal numbers() As Double) As Double
    6.     Return numbers.Min 'much cleaner than using the math.min
    7. End Function
    Whilst, agreed, these functions are much cleaner than Math.Max and Math.Min, homework assignments such as this are looking for you to demonstrate a particular technique, and in particular, you've been asked to use the Math class methods:

    Quote Originally Posted by VBclueless View Post
    Using functions to get s Minimum and Maximum that each receive three Double values and return a Double result and by using the math.min math.max.
    Also, it's been asked to specifically take three arguments, not an array of doubles. Whilst, again, the final code you've come up with is "better", it doesn't match what your assignment has asked for.

    Some other points you might want to consider:
    - If a user enters three numbers and finds the smallest/largest, and then enters different data that are invalid, the labels for smallest and largest will still contain the values from the first run. This could be confusing.
    - Your validation blocks of code are very similar. This would be an opportunity to apply a refactoring to pull out the commonalities into a separate method to avoid repetition of code.
    - 'If <some condition> = False Then' will often be written as 'If Not <some condition> Then' - following the idioms of your language is often a good thing, especially when you're starting out.
    - Indicating which datum is invalid with a background colour is a nice touch, but you never clear the colour. Also, it would be nice to indicate all the invalid data, rather than just the first one your program finds. (If textbox1 and textbox2 both contain invalid data, then only textbox1 is highlighted to the user, textbox2 is only shown as invalid once the user has corrected textbox1)

    Quote Originally Posted by mbutler755 View Post
    Using IsNumeric isn't the best practice and I would be willing to bet your teacher will tell you the same thing.
    Really? You'd be willing to gamble actual money on that? Oh how cute.

  32. #32
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Final attempt.... HELP what am i doing wrong?

    Code:
    Function highestNumber(ByVal numbers() As Double) As Double
        'using math.max
        Return Math.Max(Math.Max(numbers(0), numbers(1)), numbers(2))
    End Function
    
    Function lowestNumber(ByVal numbers() As Double) As Double
        'using math.min
        Return Math.Min(Math.Min(numbers(0), numbers(1)), numbers(2))
    End Function

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