警告:在为其分配值

时间:2015-07-15 15:30:46

标签: c# vb.net

  

“变量'line'在被赋值之前通过引用传递。在运行时可能会产生空引用异常。”

我将c#转换为VB并收到警告。我在我的c#代码中得到了结果,但不是我的VB。我假设它是因为InlineAssignHelper函数。如何在Visual Basic中正确读取和写入文件?我从未使用过VB。

static void Main(string[] args)
{
    string line;
    try
    {
        using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.csv"))
        {
            string mydirpath = "C:\\chat\\";

            string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

            foreach (string txtName in txtFileList)
            {
                string spart = ".prt";
                string sam = " AM";
                string spm = " PM";
                string sresult = "TEST RESULT: ";
                string svelocity = "MEASURED VELOCITY: ";
                string sthickness = "MEASURED THICKNESS: ";
                string smaxthickness = "MAX THICKNESS: ";
                string sminthickness = "MIN THICKNESS: ";
                string part = string.Empty;
                string date = string.Empty;
                string result = string.Empty;
                string velocity = string.Empty;
                string thickness = string.Empty;
                string maxthickness = string.Empty;
                string minthickness = string.Empty;

                using (StreamReader sr = new StreamReader(txtName))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrEmpty(line) && line.Trim().Length != 0)
                        {
                            if (line.Contains(sam) || line.Contains(spm))
                            {


                                // Every new date means a new record. If you already have data for a record, first write it.
                                if (!string.IsNullOrEmpty(date) && date.Trim().Length != 0 && !string.IsNullOrEmpty(velocity) && velocity.Trim().Length != 0 &&
                                    !string.IsNullOrEmpty(part) && part.Trim().Length != 0 && !string.IsNullOrEmpty(result) && result.Trim().Length != 0 &&
                                    !string.IsNullOrEmpty(thickness) && thickness.Trim().Length != 0 && !string.IsNullOrEmpty(maxthickness) && maxthickness.Trim().Length != 0 &&
                                    !string.IsNullOrEmpty(minthickness) && minthickness.Trim().Length != 0)
                                {
                                    // Parse DateTime format
                                    DateTime dt = Convert.ToDateTime(date);
                                    DateTime dt1 = Convert.ToDateTime(line);
                                    TimeSpan secondsdiff = dt1.Subtract(dt);

                                    double seconds = secondsdiff.TotalSeconds;

                                    int I = 8;
                                    string ckRes;
                                    if (velocity.Remove(0, 20).Length > 4)
                                    {
                                        ckRes = velocity.Remove(0, 20);
                                    }
                                    else { ckRes = "0"; }
                                    string ckThick;
                                    if (thickness.Remove(0, 20).Length > 4)
                                    {
                                        ckThick = thickness.Remove(0, 20);
                                    }
                                    else { ckThick = "0"; }
                                    string[] x = new string[I];
                                    x[0] = date;
                                    x[1] = ckRes;
                                    x[2] = part.Remove(0, 21);
                                    x[3] = result.Remove(0, 13);
                                    x[4] = ckThick;
                                    x[5] = maxthickness.Remove(0, 15);
                                    x[6] = minthickness.Remove(0, 15);
                                    x[7] = seconds.ToString();
                                    sw.WriteLine(x[0] + "," + x[1] + "," + x[2] + "," + x[3] + "," + x[4] + "," + x[5] + "," + x[6] + "," + x[7]);
                                }
                                // Then reset data to prepare it for a new record
                                part = string.Empty;
                                result = string.Empty;
                                velocity = string.Empty;
                                thickness = string.Empty;
                                minthickness = string.Empty;
                                maxthickness = string.Empty;
                                date = line;
                            }

                            if (line.Contains(spart)) { part = line; }

                            if (line.Contains(sresult)) { result = line; }

                            if (line.Contains(svelocity)) { velocity = line; }

                            if (line.Contains(sthickness)) { thickness = line; }

                            if (line.Contains(smaxthickness)) { maxthickness = line; }

                            if (line.Contains(sminthickness)) { minthickness = line; }
                        }
                    } //end while
                }

                // After last record you still have some data to write
                if (!string.IsNullOrEmpty(date) && date.Trim().Length != 0 && !string.IsNullOrEmpty(velocity) && velocity.Trim().Length != 0 &&
                    !string.IsNullOrEmpty(part) && part.Trim().Length != 0 && !string.IsNullOrEmpty(result) && result.Trim().Length != 0 &&
                    !string.IsNullOrEmpty(thickness) && thickness.Trim().Length != 0 && !string.IsNullOrEmpty(maxthickness) && maxthickness.Trim().Length != 0 &&
                    !string.IsNullOrEmpty(minthickness) && minthickness.Trim().Length != 0)
                {
                    int I = 7;
                    string ckRes;
                    if (velocity.Remove(0, 20).Length > 4)
                    {
                        ckRes = velocity.Remove(0, 20);
                    }
                    else { ckRes = "0"; }
                    string ckThick;
                    if (thickness.Remove(0, 20).Length > 4)
                    {
                        ckThick = thickness.Remove(0, 20);
                    }
                    else { ckThick = "0"; }
                    string[] x = new string[I];
                    x[0] = date;
                    x[1] = ckRes;
                    x[2] = part.Remove(0, 21);
                    x[3] = result.Remove(0, 13);
                    x[4] = ckThick;
                    x[5] = maxthickness.Remove(0, 15);
                    x[6] = minthickness.Remove(0, 15);

                    sw.WriteLine(x[0] + "," + x[1] + "," + x[2] + "," + x[3] + "," + x[4] + "," + x[5] + "," + x[6] + ",9000");
                }
            }
        }
    }
    catch
    {

    }
}
Private Shared Sub Main(ByVal args As String())
    Dim line As String
    Try
        Using sw As New StreamWriter("C:\writetest\writetest.csv")
            Dim mydirpath As String = "C:\chat\"

            Dim txtFileList As String() = Directory.GetFiles(mydirpath, "*.txt")

            For Each txtName As String In txtFileList
                Dim spart As String = ".prt"
                Dim sam As String = " AM"
                Dim spm As String = " PM"
                Dim sresult As String = "TEST RESULT: "
                Dim svelocity As String = "MEASURED VELOCITY: "
                Dim sthickness As String = "MEASURED THICKNESS: "
                Dim smaxthickness As String = "MAX THICKNESS: "
                Dim sminthickness As String = "MIN THICKNESS: "
                Dim part As String = String.Empty
                Dim [date] As String = String.Empty
                Dim result As String = String.Empty
                Dim velocity As String = String.Empty
                Dim thickness As String = String.Empty
                Dim maxthickness As String = String.Empty
                Dim minthickness As String = String.Empty

                Using sr As New StreamReader(txtName)
                    While (InlineAssignHelper(line, sr.ReadLine())) IsNot Nothing
                        If Not String.IsNullOrEmpty(line) AndAlso line.Trim().Length <> 0 Then
                            If line.Contains(sam) OrElse line.Contains(spm) Then


                                ' Every new date means a new record. If you already have data for a record, first write it.
                                If Not String.IsNullOrEmpty([date]) AndAlso [date].Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(velocity) AndAlso velocity.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(part) AndAlso part.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(result) AndAlso result.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(thickness) AndAlso thickness.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(maxthickness) AndAlso maxthickness.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(minthickness) AndAlso minthickness.Trim().Length <> 0 Then
                                    ' Parse DateTime format
                                    Dim dt As DateTime = Convert.ToDateTime([date])
                                    Dim dt1 As DateTime = Convert.ToDateTime(line)
                                    Dim secondsdiff As TimeSpan = dt1.Subtract(dt)

                                    Dim seconds As Double = secondsdiff.TotalSeconds

                                    Dim I As Integer = 8
                                    Dim ckRes As String
                                    If velocity.Remove(0, 20).Length > 4 Then
                                        ckRes = velocity.Remove(0, 20)
                                    Else
                                        ckRes = "0"
                                    End If
                                    Dim ckThick As String
                                    If thickness.Remove(0, 20).Length > 4 Then
                                        ckThick = thickness.Remove(0, 20)
                                    Else
                                        ckThick = "0"
                                    End If
                                    Dim x As String() = New String(I - 1) {}
                                    x(0) = [date]
                                    x(1) = ckRes
                                    x(2) = part.Remove(0, 21)
                                    x(3) = result.Remove(0, 13)
                                    x(4) = ckThick
                                    x(5) = maxthickness.Remove(0, 15)
                                    x(6) = minthickness.Remove(0, 15)
                                    x(7) = seconds.ToString()
                                    sw.WriteLine(x(0) + "," + x(1) + "," + x(2) + "," + x(3) + "," + x(4) + "," + x(5) + "," + x(6) + "," + x(7))
                                End If
                                ' Then reset data to prepare it for a new record
                                part = String.Empty
                                result = String.Empty
                                velocity = String.Empty
                                thickness = String.Empty
                                minthickness = String.Empty
                                maxthickness = String.Empty
                                [date] = line
                            End If

                            If line.Contains(spart) Then
                                part = line
                            End If

                            If line.Contains(sresult) Then
                                result = line
                            End If

                            If line.Contains(svelocity) Then
                                velocity = line
                            End If

                            If line.Contains(sthickness) Then
                                thickness = line
                            End If

                            If line.Contains(smaxthickness) Then
                                maxthickness = line
                            End If

                            If line.Contains(sminthickness) Then
                                minthickness = line
                            End If
                        End If
                        'end while
                    End While
                End Using

                ' After last record you still have some data to write
                If Not String.IsNullOrEmpty([date]) AndAlso [date].Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(velocity) AndAlso velocity.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(part) AndAlso part.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(result) AndAlso result.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(thickness) AndAlso thickness.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(maxthickness) AndAlso maxthickness.Trim().Length <> 0 AndAlso Not String.IsNullOrEmpty(minthickness) AndAlso minthickness.Trim().Length <> 0 Then
                    Dim I As Integer = 7
                    Dim ckRes As String
                    If velocity.Remove(0, 20).Length > 4 Then
                        ckRes = velocity.Remove(0, 20)
                    Else
                        ckRes = "0"
                    End If
                    Dim ckThick As String
                    If thickness.Remove(0, 20).Length > 4 Then
                        ckThick = thickness.Remove(0, 20)
                    Else
                        ckThick = "0"
                    End If
                    Dim x As String() = New String(I - 1) {}
                    x(0) = [date]
                    x(1) = ckRes
                    x(2) = part.Remove(0, 21)
                    x(3) = result.Remove(0, 13)
                    x(4) = ckThick
                    x(5) = maxthickness.Remove(0, 15)
                    x(6) = minthickness.Remove(0, 15)

                    sw.WriteLine(x(0) + "," + x(1) + "," + x(2) + "," + x(3) + "," + x(4) + "," + x(5) + "," + x(6) + ",9000")
                End If
            Next
        End Using

    Catch
    End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function

3 个答案:

答案 0 :(得分:1)

你真正应该做的是改用StreamReader.EndOfStream。将您的while循环转换为:

While sr.EndOfStream = false
    line = sr.ReadLine()
    ' rest of code'
End While

这将使代码更易于阅读,您也可以在C#中使用相同的习惯用法。

答案 1 :(得分:0)

在使用它之前,您似乎正确地检查它是否为空,但只是因此编译器没有抱怨您可以将line设置为占位符值,如""

答案 2 :(得分:0)

While (InlineAssignHelper(line, sr.ReadLine())) IsNot Nothing

这应该更像 -

While(true)
line = sr.ReadLine()
if Not object.Equals(line, nothing) 'continue with the rest of your code...

另外,正如其他人所说,您的line变量应设置为"",您也应该抓住Catch块中的exec。

半相关的MSDN文章 - https://msdn.microsoft.com/en-us/library/system.io.stringreader.readline(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1代码在页面底部的snippit有点显示了我所得到的......

编辑:作为一个脚注,这是我最好的猜测,如果没有OP实际在调试器中正确运行代码,并且不知道在catch语句中会抛出什么错误。