2012-04-03 129 views
-1

從MSDN文章我讀我應該使用StringBuilder而不是串聯一個正常的字符串。但是我不知道爲什麼我會得到下面的錯誤:「在變量'ShowString'被使用之前,它已經被分配了一個值。在運行時會導致一個空引用異常。在賦值之前使用變量?

下面的代碼:

Dim ShowString As StringBuilder 
    Dim ShowSort As StringBuilder 
    'ShowString. 
    ShowString.Append("POS,tdate,Product") 
    '========Show Options================== 
    If CheckBox1.Checked = True Then 
     ShowString.Append(",tkey") 
    End If 
    If CheckBox2.Checked = True Then 
     ShowString.Append(",Price") 
    End If 
    If CheckBox3.Checked = True Then 
     ShowString.Append(",FID") 
    End If 
    '==========End Show Options============ 

    '=========Sort Options================ 
    If RadioButton1.Checked = True Then 
     ShowSort.Append("tdate") 
     If RadioButton8.Checked Then 
      ShowSort.Append(" desc") 
     End If 
    End If 
    If RadioButton2.Checked = True Then 
     ShowSort.Append("tkey") 
     If RadioButton8.Checked Then 
      ShowSort.Append(" desc") 
     End If 
    End If 
    '=======End Sort Options============= 
    Dim sort As String = ShowSort.ToString 
    Dim show As String = ShowString.ToString 
    Try 
     con.Open() 
    Catch ex As Exception 
     MessageBox.Show("Please contact support, there was a database error with the following message: " & ex.Message, "Cannot Connect", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End Try 
+0

無關,與SB。 – Will 2012-04-03 15:50:43

回答

6

你需要一個StringBuilder對象分配給引用:

Dim ShowString As StringBuilder = New StringBuilder() 

或者:

Dim ShowString As New StringBuilder() 
+0

5秒鐘打我:) – 2012-04-03 15:50:03

+0

noob錯誤由我,謝謝! – 2012-04-03 15:55:33