将C#转换为VBNET时委派错误

时间:2012-11-02 11:10:36

标签: c# .net mysql vb.net

我在c#

中有这段代码
void Backup()
{
    string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
    string file = "C:\\MyDumpFile.sql";
    MySqlBackup mb = new MySqlBackup(constr);
    mb.ExportInfo.FileName = file;
    mb.ExportProgressChanged += new MySqlBackup.exportProgressChange(mb_ExportProgressChanged);
    mb.ExportCompleted += new MySqlBackup.exportComplete(mb_ExportCompleted);
    timerRead.Start();
    mb.Export();
}

转换为VBNet

Private Sub Backup()
    Dim constr As String = "server=localhost;user=root;pwd=qwerty;database=test;"
    Dim file As String = "C:\MyDumpFile.sql"
    Dim mb As New MySqlBackup(constr)
    mb.ExportInfo.FileName = file
    mb.ExportProgressChanged = mb.ExportProgressChanged + New MySqlBackup.exportProgressChange(mb_ExportProgressChanged)
    mb.ExportCompleted = mb.ExportCompleted + New MySqlBackup.exportComplete(mb_ExportCompleted)
    timerRead.Start()
    mb.Export()
End Sub

我总是收到此错误

  

'exportProgressChange'是一种类型   'MySql.Data.MySqlClient.MySqlBackup'并不能用作   表达

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

Private Sub Backup()
Dim constr As String = "server=localhost;user=root;pwd=qwerty;database=test;"
Dim file As String = "C:\MyDumpFile.sql"
Dim mb As New MySqlBackup(constr)
mb.ExportInfo.FileName = file
AddHandler mb.ExportProgressChanged, New MySqlBackup.exportProgressChange(AddressOf mb_ExportProgressChanged)
AddHandler mb.ExportCompleted, New MySqlBackup.exportComplete(AddressOf mb_ExportCompleted)
timerRead.Start()
mb.Export()
End Sub

应该有效,因为在VB .net中你不能使用+ =来订阅一个事件,你必须使用AddHandlerRemoveHandler

编辑:尝试过,所以编译得很好。