2011-09-05 35 views
0

我有這樣的代碼:如何將後期綁定語法轉換爲vb.net中的早期綁定語法?

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Try 
     ' retrieving the administration table. 
     con.Open() 
     DataAdapter1.SelectCommand = sqladmin 
     DataAdapter1.Fill(ds, "stratos") 
     DataGridView1.DataSource = ds 
     DataGridView1.DataMember = "stratos" 
     con.Close() 
    Catch myerror As MySqlException 
     MessageBox.Show("Error Retrieving Administration Table: " & myerror.Message) 
    End Try 


    Try 
     ' retrieving the projects list. 
     con.Open() 
     DataAdapter2.SelectCommand = sqlprojects 
     DataAdapter2.Fill(ds2, "projects") 
     ListBox1.Items.Clear() 

     For Each DataRow In ds2.Tables("projects").Rows 

      ' ##### THE ERROR OCCURS ON THE LINE BELOW: ##### ' 

      ListBox1.Items.Add(DataRow("project_name")) 
     Next 
     con.Close() 

    Catch myerror As MySqlException 
     MessageBox.Show("Error Retrieving Projects List: " & myerror.Message) 
    End Try 

,並即時得到以下錯誤:

Error 1: Option Strict On disallows late binding.

進出口使用上運行的Windows 7操作系統 一個GATEWAY筆記本多數民衆贊成的Visual Basic 2010速成如何解決這個錯誤?

回答

3

你需要有錯誤的循環更改爲以下:

For Each dr as DataRow In ds2.Tables("projects").Rows 

    ' ##### THE ERROR OCCURS ON THE LINE BELOW: ##### ' 

    ListBox1.Items.Add(Convert.ToString(dr("project_name"))) 
Next 
2

這通常意味着你Dim'd一個變量或聲明一個沒有類型的函數。您的For Each循環中使用的DataRow可能是罪魁禍首。另一個是DatRow中的項目是類型對象;你應該施放它們或以其他方式將它們轉換爲特定類型。你想這個代替:

For Each dr As DataRow in d2.Tables("projects").Rows 
    '... 

    ListBox1.Items.Add(dr("project_name").ToString()) 
Next dr 
+0

正確的答案是,ListBox1.Items.Add不需要String類型。可以添加對象並調用它們的.ToString()方法,除非有自定義的DrawItem實現。 –