2012-04-03 10 views
-1

MSDN記事から、通常の文字列を連結するのではなく、StringBuilderを使用する必要があります。しかし、なぜ私は次のエラーが出るのかわかりません: "変数 'ShowString'は、値が割り当てられる前に使用されています。実行時にnull参照例外が発生する可能性があります。変数に値を割り当てる前に変数を使用していますか?

次のコード:

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

何を。 – Will

答えて

6

あなたが参照にStringBuilderオブジェクトを割り当てる必要がありません:

Dim ShowString As StringBuilder = New StringBuilder() 

または:SBを行うには

Dim ShowString As New StringBuilder() 
+0

5秒で私を打つ:) –

+0

私のおかげでnoob間違い、ありがとう! –

関連する問題