Lowest Solution

problem in program and displaying lowest score

Page 1 of 1

8 Replies - 1140 Views - Last Post: 22 March 2008 - 06:54 PM Rate Topic: -----

#1 georgecasino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-February 08

Lowest Solution

Post icon  Posted 21 March 2008 - 05:25 PM

Public Class MainForm

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub displayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles displayButton.Click
        ' displays the lowest value stored in an array

        Dim scores() As Integer = {78, 87, 95, 94, 77, 82, 81, 82, 65, 45, _
                                   66, 54, 32, 24, 100, 78, 86, 13, 92, 91}
        Dim searchFor As String
        Dim subscript As Integer

        'assign lowest score to a variable
        searchFor = lowestLabel.Text

        'search the scores array for the lowest score
        'continue searching until there are
        'no more array elements to search or
        'the lowest score is found
        Do Until subscript = scores.Length _
            OrElse searchFor = scores(subscript)
            subscript = subscript - 1

        Loop

        'determine whether the score
        'was found in the scores array
        If subscript < scores.Length Then
            Label1.Text = scores(subscript).ToString("C0")
        Else
            MessageBox.Show("Invalid score.", _
                "Lowest Project", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
        End If

        lowestLabel.Focus()


    End Sub
End Class



Error 1 Option Strict On disallows implicit conversions from 'String' to 'Double'. C:\Course Technology\83623-0\VbReloaded\Chap08\Lowest Solution\Lowest Project\Main Form.vb 30 20 Lowest Project

I cannot figure out how the lowest score will be displayed nor do I know why I am getting this error. Can someone please help me figure this out?

This post has been edited by PsychoCoder: 21 March 2008 - 05:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Lowest Solution

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Lowest Solution

Posted 21 March 2008 - 06:17 PM

Post the entire error message as it will indicate which line contains the error.
Was This Post Helpful? 0
  • +
  • -

#3 georgecasino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-February 08

Re: Lowest Solution

Posted 21 March 2008 - 07:18 PM

View Postjayman9, on 21 Mar, 2008 - 06:17 PM, said:

Post the entire error message as it will indicate which line contains the error.


Error 1 Option Strict On disallows implicit conversions from 'String' to 'Double'. C:\Course Technology\83623-0\VbReloaded\Chap08\Lowest Solution\Lowest Project\Main Form.vbLine
Line Column
30 20 Lowest Project
Was This Post Helpful? 0
  • +
  • -

#4 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

Re: Lowest Solution

Posted 21 March 2008 - 08:17 PM

This was the offending line
OrElse searchFor = scores(subscript)


scores is an array of integers, so scores(subscript) is an integer, but searchFor is a string, hence the error.

Try using an integer for your searchFor variable
Dim searchFor As Integer


and use this (or the Convert.ToInt32() function)
searchFor = Int32.Parse(lowestLabel.Text)


then instead of counting down, count up
subscript = subscript + 1



I know without explicit assignment, the value of an integer variable is zero. But it's always good programming practice to set it.
Dim subscript As Integer = 0



Actually, a normal for or while loop works just fine. It's probably easier to read than a do-until loop...

This post has been edited by orcasquall: 21 March 2008 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

#5 georgecasino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-February 08

Re: Lowest Solution

Posted 22 March 2008 - 02:10 PM

Thanks for the help. Now I am getting a FormatException was unhandled
searcFor = Int32.Parse(lowestLabel.Text)
I want it to display the score 13. I might have an idea on how to do that, but it does not go with the exception. When I put a ("13") instead of the lowestLabel.Text and run it, th einterface shows 78 where lowest score should be and it is not displaying in the box. So I am trying to figure out how do I clear the exception and after clearing that up if I will be able to make it display the number 13. What do you think?


Private Sub displayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles displayButton.Click
        ' displays the lowest value stored in an array

        Dim scores() As Integer = {78, 87, 95, 94, 77, 82, 81, 82, 65, 45, _
                                   66, 54, 32, 24, 100, 78, 86, 13, 92, 91}
        Dim searchFor As Integer
        Dim subscript As Integer = 0

        'assign lowest score to a variable
        searchFor = Int32.Parse(lowestLabel.Text)



        'search the scores array for the lowest score
        'continue searching until there are
        'no more array elements to search or
        'the lowest score is found
        Do While subscript = scores.Length _
            OrElse searchFor = scores(subscript)
            subscript = subscript + 1

        Loop

        'determine whether the score
        'was found in the scores array
        If subscript < scores.Length Then
            Label1.Text = scores(subscript).ToString("C0")
        Else
            MessageBox.Show("Invalid score.", _
                "Lowest Project", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
        End If

        lowestLabel.Focus()


    End Sub
End Class



Please use code tags when posting your code => :code: Also, there is no need to quote the entire post above the one you're replying to :)

This post has been edited by PsychoCoder: 22 March 2008 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Lowest Solution

Posted 22 March 2008 - 03:09 PM

Just using Parse will throw an exception if you enter something other than a numeric value. I suggest using TryParse as it will deal with any exceptions for you, well it will either convert it or it wont, but it wont throw an exception

Private Sub displayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles displayButton.Click
        ' displays the lowest value stored in an array

        Dim scores() As Integer = {78, 87, 95, 94, 77, 82, 81, 82, 65, 45, _
                                   66, 54, 32, 24, 100, 78, 86, 13, 92, 91}
        Dim searchFor As Integer
        Dim subscript As Integer = 0
        Dim converted As Boolean
        'assign lowest score to a variable. Use TryParse to convert the
        'value. TryParse wont throw an exception if the conversion fails
        converted= Integer.TryParse(lowestLabel.Text,searchFor)


        'Now check whether the value was converted
        If converted Then
              'search the scores array for the lowest score
              'continue searching until there are
              'no more array elements to search or
              'the lowest score is found
              Do While subscript = scores.Length  OrElse searchFor = scores(subscript)
                     subscript = subscript + 1
               Loop

                'determine whether the score
                'was found in the scores array
                If subscript < scores.Length Then
                       Label1.Text = scores(subscript).ToString("C0")
                Else
                        MessageBox.Show("Invalid score.", _
                         "Lowest Project", MessageBoxButtons.OK, _
                          MessageBoxIcon.Information)
                 End If

                 lowestLabel.Focus()
        Else
                MessageBox.Show("You must enter a numeric value")
        End If
End Sub




Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#7 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: Lowest Solution

Posted 22 March 2008 - 05:32 PM

Just turn strict off
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Lowest Solution

Posted 22 March 2008 - 06:31 PM

He cant turn strict off. I helped someone with this same exact assignment not too long ago and they were required to have strict on. Plus, I always push people towards TryParse over Parse, I never use Parse because then you have to check for errors, with TryParse you dont have to.
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Lowest Solution

Posted 22 March 2008 - 06:54 PM

Strict On and Explicit On are always recommended when creating solutions, as they help promote consistent and safe code in a program.

Quote

Visual Basic allows conversions of many data types to other data types. Data loss can occur when the value of one data type is converted to a data type with less precision or smaller capacity. A run-time error occurs if such a narrowing conversion fails. Option Strict ensures compile-time notification of these narrowing conversions so they can be avoided.


Quote

When Option Explicit appears in a file, you must explicitly declare all variables using the Dim or ReDim statements. If you attempt to use an undeclared variable name, an error occurs at compile time.

Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear. If you do not use the Option Explicit statement, all undeclared variables are of Object type.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1