在赋值之前使用变量'details'。

时间:2014-04-23 18:09:09

标签: vb.net rowdetails

在为变量details分配值之前使用它。 details有什么问题?

Option Explicit On
Imports System.Text
Imports System.IO
Public Class Main
    Private SelectedItem As ListViewItem
    Dim data As String

    Dim strpriority As String
    Dim task As String
    Dim createdate As String
    Dim duedate As String

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        AddTask.Show()
        Me.Hide()
    End Sub

    Private Sub HistoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HistoryToolStripMenuItem.Click
        History.Show()
        Me.Hide()

    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fpath As String
        Dim splitdata
        fpath = AppDomain.CurrentDomain.BaseDirectory
        Dim filepath As String
        filepath = fpath & "task.txt"
        Dim details As String
        details = My.Computer.FileSystem.ReadAllText(filepath)
        splitdata = Split(details, vbCrLf)
        Dim i As Integer
        For i = 0 To UBound(splitdata)
            lblTaskName.Items.Add(splitdata(i))
        Next

        lblTime.Enabled = True
        Timer1.Interval = 10
        Timer1.Enabled = True
        lblDate.Text = DateTime.Now.ToString("dd MMMM yyyy")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblTime.Text = TimeOfDay
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If lblTaskName.SelectedItem = "" Then
            MsgBox("Please select a record")

        Else

            If lblTaskName.Items.Count > 0 Then
                If MessageBox.Show("Do you really want to delete this record?", "Delete", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
                    lblTaskName.Items.Remove(lblTaskName.SelectedItem.ToString())

                Else
                    MessageBox.Show("Operation Cancelled")

                End If
            End If

        End If

        Try

            Dim fpath As String
            fpath = AppDomain.CurrentDomain.BaseDirectory
            Dim filepath As String
            filepath = fpath & "task.txt"
            Dim details As String
            If lblTaskName.Items.Count > 0 Then
                details = lblTaskName.Items(0)
                Dim i As Integer
                For i = 1 To lblTaskName.Items.Count - 1

                    details = details & vbCrLf & lblTaskName.Items(i)

                Next
            End If
            My.Computer.FileSystem.WriteAllText(filepath, details, False)
        Catch ex As Exception
            MsgBox("Values Can't be inserted this time")
        End Try
    End Sub

    Private Function filepaths() As String
        Throw New NotImplementedException
    End Function
End Class

2 个答案:

答案 0 :(得分:1)

问题在于此部分的btnRemove_Click方法:

Dim details As String
If lblTaskName.Items.Count > 0 Then
    details = lblTaskName.Items(0)

如果条件的计算结果为false,则在初始化之前使用details变量,因为它仅在if块中设置到目前为止。

我想您想将以下行移到if块中以解决问题:

My.Computer.FileSystem.WriteAllText(filepath, details, False)

或者,您可以为details提供默认值,以便在任何情况下都设置它。出于性能原因,您可以在String.Empty分支中设置默认值(例如文本或else):

Dim details As String
If lblTaskName.Items.Count > 0 Then
     ' ...
Else
    details = "Default Value"
End If

答案 1 :(得分:1)

您需要仔细考虑您的计划流程。请考虑以下代码:

Dim details As String
If lblTaskName.Items.Count > 0 Then
    details = lblTaskName.Items(0)
    Dim i As Integer
    For i = 1 To lblTaskName.Items.Count - 1
        details = details & vbCrLf & lblTaskName.Items(i)
    Next
End If
My.Computer.FileSystem.WriteAllText(filepath, details, False)

您在顶部声明details变量。然后检查lblTaskName控件中是否至少有一个项目。如果该测试通过,则将第一个项目分配给details但是,如果该测试没有通过怎么办?如果lblTaskName控件中有0个项怎么办?在这种情况下,If块的内部永远不会运行,并且任何内容都不会存储在details中。然后在最后一行中,您尝试使用details块之外的If变量*的值。这是非法的,因为它可能没有分配值。

也许你的意思是WriteAllText行是If块里面的?否则,您需要在Else语句中添加If子句,以处理lblTaskName中有0项的情况。


除此之外,从风格上讲,您应该尽可能在声明时初始化变量。例如,而不是写:

    Dim fpath As String
    Dim splitdata
    fpath = AppDomain.CurrentDomain.BaseDirectory
    Dim filepath As String
    filepath = fpath & "task.txt"
    Dim details As String
    details = My.Computer.FileSystem.ReadAllText(filepath)
    splitdata = Split(details, vbCrLf)

将其写为:

    Dim fpath As String       = AppDomain.CurrentDomain.BaseDirectory
    Dim filepath As String    = fpath & "task.txt"
    Dim details As String     = My.Computer.FileSystem.ReadAllText(filepath)
    Dim splitdata() As String = Split(details, vbCrLf)

(我是强迫症,所以我排列了我的等号。那部分是完全可选的。)

它不会使代码运行得更快,但它确实使它更容易阅读!更重要的是,它减少了漏洞的可能性。