简体   繁体   English

什么是本代码的VB.NET版本?

[英]What is VB.NET Version of this Code?

if (InvokeRequired)
{
  BeginInvoke(new MethodInvoker(delegate()
  {
    textBox1.Text = args.Fax.Port.ToString();
    textBox2.Text = args.Fax.FaxStatus.ToString();
  }));
}

Courtesy of developerfusion.com : developerfusion.com提供

If InvokeRequired Then
    BeginInvoke(New MethodInvoker(Function() Do
        textBox1.Text = args.Fax.Port.ToString()
        textBox2.Text = args.Fax.FaxStatus.ToString()
    End Function))
End If

From MSDN 来自MSDN

Delegate Sub MyDelegate(myControl As Label, myArg2 As String)

Private Sub Button_Click(sender As Object, e As EventArgs)
   Dim myArray(1) As Object

   myArray(0) = New Label()
   myArray(1) = "Enter a Value"
   myTextBox.BeginInvoke(New MyDelegate(AddressOf DelegateMethod), myArray)
End Sub 

Public Sub DelegateMethod(myControl As Label, myCaption As String)
   myControl.Location = New Point(16, 16)
   myControl.Size = New Size(80, 25)
   myControl.Text = myCaption
   Me.Controls.Add(myControl)
End Sub 

So 所以

if (InvokeRequired)
{
  BeginInvoke(new MethodInvoker(delegate()
  {
    textBox1.Text = args.Fax.Port.ToString();
    textBox2.Text = args.Fax.FaxStatus.ToString();
  }));
}

Delegate Sub MyDelegate(faxPort As String, faxStatus As String)

If InvokeRequired Then
    Dim aArray(1) as Object
    aArray(0) = args.Fax.Port.ToString()
    aArray(1) = args.Fax.FaxStatus.ToString();
    BeginInvoke(New MyDelegate(AddressOf MySub), aArray)
End If

Sub MySub( faxPort as String, faxStatus as String)
    textBox1.Text = faxPort
    textBox2.Text = faxStatus
End Sub

I think 我认为

VB.NET 9 and previous does not support multi line anonymous functions. VB.NET 9和之前的版本不支持多行匿名函数。 You need to write a separate function: 你需要编写一个单独的函数:

If InvokeRequired Then
    BeginInvoke(New MethodInvoker(AddressOf MySub))
End If

where MySub: MySub的地方:

Sub MySub()
    textBox1.Text = args.Fax.Port.ToString()
    textBox2.Text = args.Fax.FaxStatus.ToString()    
End Sub

UPDATE: 更新:

To clarify about the args parameter there's an overload of BeginInvoke that could be used and in this case the MethodInvoker delegate is no longer suitable. 为了澄清args参数,可以使用BeginInvoke的重载,在这种情况下, MethodInvoker委托不再适用。 Action(Of T) would work: 行动(T)将起作用:

If InvokeRequired Then
    BeginInvoke(New Action(Of SomeType)(AddressOf MySub), args)
End If

and: 和:

Sub MySub(ByVal args as SomeType)
    textBox1.Text = args.Fax.Port.ToString()
    textBox2.Text = args.Fax.FaxStatus.ToString()    
End Sub

After all it's simple things like this that make C# developers happy :-) (please don't take it wrong, I have nothing against VB.NET) 毕竟这样的简单事情让C#开发人员高兴:-)(请不要误解,我对VB.NET没有任何反对意见)

Until Visual Studio 2010, VB.Net does not support multi-statement anonymous functions. 在Visual Studio 2010之前,VB.Net不支持多语句匿名函数。

You need to move the anonymous method into a separate method that takes args as a parameter, then call Invoke on it. 您需要将匿名方法移动到一个单独的方法,该方法将args作为参数,然后在其上调用Invoke

Since the comments do not allow formatting, I'm going to post my working solution here. 由于评论不允许格式化,我将在此处发布我的工作解决方案。 Thanks to all who chimed in - Darin, SLaks, drachenstern, Jay. 感谢所有插话的人 - 达林,SLaks,drachenstern,Jay。

Private Sub myControl_FaxStatus(ByVal sender As Object, ByVal args As DataTech.FaxManNet.FaxEventArgs)
  frmModemStatus.UpdateStatus(args.Fax)

  If InvokeRequired Then
    Dim aArray(1) As Object
    aArray(0) = args.Fax.Port.ToString()
    aArray(1) = args.Fax.FaxStatus.ToString()
    BeginInvoke(New MyDelegate(AddressOf UpdateStatusDisplay), aArray)
  End If

  Try
    TextBox1.Text = args.Fax.Port.ToString()
    TextBox2.Text = args.Fax.FaxStatus.ToString()
  Catch ex As Exception
    System.Diagnostics.Debug.WriteLine("ex = " & ex.ToString())
  End Try

End Sub

Delegate Sub MyDelegate(ByVal faxPort As String, ByVal faxStatus As String)

Private Sub UpdateStatusDisplay(ByVal faxPort As String, ByVal faxStatus As String)
  TextBox1.Text = faxPort
  TextBox2.Text = faxStatus
End Sub

If you need similar stuff in the feature just install the following component to your visual studio. 如果您需要在功能中使用类似的东西,只需将以下组件安装到Visual Studio中即可。 After that you can easly convert from c# to vb.net or vb.net to c#. 之后,您可以轻松地将c#转换为vb.net或vb.net转换为c#。 VS2010 Support is already included. VS2010支持已包含在内。 http://codeconvert.codeplex.com/ http://codeconvert.codeplex.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM