我需要帮助修复我的代码! [英] I need help fixing my code!

查看:78
本文介绍了我需要帮助修复我的代码!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!所以我对VB很新,但觉得我很好。我差不多完成了一项任务,我在这最后一部分遇到了麻烦。

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.

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



我的问题在于code


My problem is in the code

Dim Workshop As String = lstWorkshop.SelectedItem
Dim Location As String lstLocation.SelectedItem



两行都说Option Strict On禁止从'object'到'string'的隐式转换我很乐意只是指出正确的方向。谢谢!



我尝试了什么:



我试过改变


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

Dim Workshop As String = lstWorkshop.SelectedItem
Dim Location As String lstLocation.SelectedItem



进入


into

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.

推荐答案

ListBox.SelectedItem属性(System.Windows.Forms) [ ^ ]是一个对象类型。



所以你必须使用 ToString() 方法:

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

So you have to use the ToString() method:
Dim Workshop As String = lstWorkshop.SelectedItem.ToString()


Option Strict is一个好主意 - 它允许您在编译时捕获问题,而不是在运行时 - 这意味着您有时不会在发现它错误之前执行代码。总是留下它的好主意。



它抱怨的一件事就是当你这样做时:

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:
Dim o as Object = ...
Dim s as String = o

因为尽管所有字符串都是从Object派生的(事实上所有类都是如此)并不是所有的对象都是字符串 - 因此当你尝试将一个Object分配给一个String变量时,系统会说你确定这是对的吗?o可能不是字符串,所以这段代码可能会因运行时间而失败。



如果是你的代码, SelectedItem属性总是返回一个Object(因为它不知道你可能在那里填充了什么)所以你需要显式地将它转换为String(或者你放在那里的任何东西,它根本不必是一个String) )使用它:

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])


更改:

Change:
For x As Integer = 0 To lstCosts.Items.Count - 1
    sum += CDbl(lstCosts.Items(x))



to:


to:

For x As Integer = 0 To lstCosts.Items.Count - 1
    sum += CDbl(CStr(lstCosts.Items(x)))


这篇关于我需要帮助修复我的代码!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆