Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! So I am very new to VB but feel like I am picking it up pretty well. I am almost done with an assignment I am just having trouble with this last part.
VB
Public Class Form1
    Private Sub btnreset_Click(sender As Object, e As EventArgs) Handles btnreset.Click
        'Reset the Workshop and Location list boxes.
        lstWorkshop.SelectedIndex = -1
        lstLocations.SelectedIndex = -1

        'Reset the List of Costs list box.
        lstCosts.Items.Clear()

        'Reset the Total Cost label
        lblTotalCost.Text = String.Empty

        ' Return the focus to Pick a Workshop.
        lstWorkshop.Focus()

    End Sub

    Private Sub btnAddWorkshop_Click(sender As Object, e As EventArgs) Handles btnAddWorkshop.Click
        'Declare variables and assign values for both listboxes
        Dim intFee As Integer
        Dim intTotal As Integer
        Dim intDay As Integer
        Dim intStay As Integer
        Dim Workshop As String = lstWorkshop.SelectedItem
        Dim Location As String = lstLocations.SelectedItem

        If lstWorkshop.SelectedItem Is Nothing Or lstLocations.SelectedItem Is Nothing Then
            MessageBox.Show("Select both a workshop and a location")
            Return
        End If

        ' Select workshop
        Select Case Workshop
            Case CStr("Handling Stress")
                intDay = 3
                intFee = 595
            Case CStr("Time Management")
                intDay = 3
                intFee = 695
            Case CStr("Supervision Skills")
                intDay = 3
                intFee = 995
            Case CStr("Negotiation")
                intDay = 5
                intFee = 1295
            Case CStr("How to Interview")
                intDay = 1
                intFee = 395

        End Select

        ' Select location.
        Select Case Location
            Case CStr("Austin")
                intStay = intDay * 95
            Case CStr("Chicago")
                intStay = intDay * 125
            Case CStr("Dallas")
                intStay = intDay * 110
            Case CStr("Orlando")
                intStay = intDay * 100
            Case CStr("Phoenix")
                intStay = intDay * 92
            Case CStr("Raleigh")
                intStay = intDay * 90

        End Select


        'Cost of each selected workshop and location
        intTotal = intStay + intFee

        'Display cost of workshop and location in "List Cost" list box
        lstCosts.Items.Add(intTotal.ToString("c"))

    End Sub

    Private Sub lstLocations_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstLocations.SelectedIndexChanged

    End Sub

    Private Sub btnCalculateTotal_Click(sender As Object, e As EventArgs) Handles btnCalculateTotal.Click
        Dim dblTotal As Double = 0.0   'Accumulator
        Dim sum As Double           ' 

        For x As Integer = 0 To lstCosts.Items.Count - 1
            sum += CDbl(lstCosts.Items(x))
        Next
        lblTotalCost.Text = sum.ToString("c")

    End Sub
End Class

My problem is in the code
VB
Dim Workshop As String = lstWorkshop.SelectedItem
Dim Location As String lstLocation.SelectedItem

Both lines say "Option Strict On disallows implicit conversions from 'object' to 'string'" i would love to just be pointed in the right direction for this. Thanks!

What I have tried:

I have tried changing
VB
Dim Workshop As String = lstWorkshop.SelectedItem
Dim Location As String lstLocation.SelectedItem

into
VB
Dim Location As String = CType(lstLocations.SelectedItem, String)
Dim Workshop As String = CType(lstWorkshop.SelectedItem, String)

which worked well initially but caused an issue when I did my final calculation to add up the total.
Posted
Updated 26-Sep-17 22:33pm
Comments
Graeme_Grant 27-Sep-17 4:11am    
After the change above, what is happening? How does it "cause an issue when I did my final calculation to add up the total"?

Set a breakpoint on this line:
Select Case Workshop

Run the code, select the item and hit the breakpoint. Now step through the code and watch what happens.
Member 13432273 27-Sep-17 4:19am    
Sorry that was pretty unclear. When I do
Dim Location As String = CType(lstLocations.SelectedItem, String)
Dim Workshop As String = CType(lstWorkshop.SelectedItem, String)
it works until I hit the "Calculate Total" button and then I get "System.InvalidCastException: 'Conversion from string " " to type 'Double' is not valid.'
Inner Exception
FormatException: Input string was not in a correct format.
Graeme_Grant 27-Sep-17 4:27am    
Thanks for the exception details. Does it throw the exception on this line:
CDbl(lstCosts.Items(x))

The ListBox.SelectedItem Property (System.Windows.Forms)[^] is an Object type.

So you have to use the ToString() method:
VB
Dim Workshop As String = lstWorkshop.SelectedItem.ToString()
 
Share this answer
 
Comments
Graeme_Grant 27-Sep-17 4:27am    
You were too quick to answer...
Member 13432273 27-Sep-17 4:30am    
This works just as the code:
 Dim Location As String = CType(lstLocations.SelectedItem, String)
Dim Workshop As String = CType(lstWorkshop.SelectedItem, String)

worked. I now have an error at the line
   
For x As Integer = 0 To lstCosts.Items.Count - 1
            sum += CDbl(lstCosts.Items(x))

I am getting an error saying
" System.InvalidCastException: 'Conversion from string " " to type 'Double' is not valid.' "
Jochen Arndt 27-Sep-17 4:45am    
Similar problem.
The CDbl() cast (or Convert.ToDouble() requires a string argument but you are passing an Object again. So you have to convert the item object to a string first.

Sorry that I overlooked the "add up the total" part. I was focused on the first error.
Graeme_Grant 27-Sep-17 4:46am    
See my comment in solution 3 below... VB is more forgiving than C#.
Jochen Arndt 27-Sep-17 4:59am    
You are right: I was too fast and did not wait for additional information.

But at least I and Griff explained what happened and that was what he asked for:
"Both lines say "Option Strict On disallows implicit conversions from 'object' to 'string'" i would love to just be pointed in the right direction for this".
Option Strict is a good idea - it lets you catch problems at compile time, rather than at runtime - which means you don't have to execute the code before you find out it's "wrong" sometimes. Good idea to always leave it on.

One of the things it complains about is when you do this:
VB
Dim o as Object = ...
Dim s as String = o
Because although all Strings are derived from Object (as are all classes in fact) not all Objects are Strings - so when you try to assign an Object to a String variable the system is saying "Are you sure this is right? "o" may not be a String, so this code could fail as run time."

In the case of your code, the SelectedItem property always returns an Object (because it has no idea what you might have stuffed in there) so you need to explicitly cast it to a String (or whatever you put in there, it doesn't have to be a String at all) to use it:
Dim Workshop As String = CStr(lstWorkshop.SelectedItem)
Or
Dim Workshop As String = DirectCast(lstWorkshop.SelectedItem, [String])
 
Share this answer
 
Change:
VB
For x As Integer = 0 To lstCosts.Items.Count - 1
    sum += CDbl(lstCosts.Items(x))

to:
For x As Integer = 0 To lstCosts.Items.Count - 1
    sum += CDbl(CStr(lstCosts.Items(x)))
 
Share this answer
 
Comments
Member 13432273 27-Sep-17 4:38am    
I did this but I got the same Exception Unhandled error
System.InvalidCastException: 'Conversion from string " " to type 'Double' is not valid.'
Inner Exception
FormatException: Input string was not in a correct format.
Graeme_Grant 27-Sep-17 4:45am    
This works:
Dim num As Double = 123456789.123
Dim numString As Object = DirectCast(num.ToString("c"), Object)
Dim newNum As Double = CDbl(numString)

This will throw your error:
Dim num As Double = 123456789.123
Dim numString As Object = DirectCast(" ", Object)
Dim newNum As Double = CDbl(numString)

This is because you can not convert " " (a space) to a number. The value is not of the correct type. You need to check why you have an invalid value in your list.
Member 13432273 27-Sep-17 4:56am    
So I am following what you're saying but I am struggling to find how i can apply it to my code. Should I be creating new variables in addition to the ones i have or just editing the ones i have?
Graeme_Grant 27-Sep-17 4:59am    
Do you know how to set a breakpoint? And how to step over code? And how to use intellisense or the Debug Locals window to check your values?

Another way to check values is to do the following:
For x As Integer = 0 To lstCosts.Items.Count - 1
    Debug.WriteLine(String.Format("{0}: [{1}]", x, lstCosts.Items(x))
    ' sum += CDbl(CStr(lstCosts.Items(x)))
Next

Now you watch the Debug Output window and you will see what your list contains.
Member 13432273 27-Sep-17 5:01am    
I'm sorry I have no idea how to do any of that and if I do then I don't recognize any of the terminology

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900