需要帮助将此代码段从c#转换为vb

时间:2011-01-27 21:23:04

标签: c# vb.net

嘿,我已经尝试了所有的在线代码转换器,但没有一个能够解决这个问题。有人可以向我解释一下如何将它变形为vb.net吗?

yahoo.OnBuddyAddYouRequest += delegate(object sender, string buddy, string requestMessage, ref bool bAccept)
{
    object[] inVar = { sender, buddy, requestMessage,bAccept };
    this.Invoke(new OnBuddyAddYouRequestYahooEventHandler(OnBuddyAddYouRequest), inVar);
    bAccept = (bool)inVar[3];
};

1 个答案:

答案 0 :(得分:2)

将内部代码更改为单独的方法:

private void buddyAddYouRequest(object sender, string buddy,
        string requestMessage, ref bool bAccept)
{
    object[] inVar = { sender, buddy, requestMessage, bAccept };
    this.Invoke(new OnBuddyAddYouRequestYahooEventHandler(OnBuddyAddYouRequest), inVar);
    bAccept = (bool)inVar[3];
}

然后将您引用的行更改为:

yahoo.OnBuddyAddYouRequest += buddyAddYouRequest;

然后您可以通过代码转换器运行它以将其转换为VB.NET。

但是,我应该指出代码没有意义。它似乎调用了一个现有的方法OnBuddyAddYouRequest,但是以一种不必要的迂回方式。我意识到作者可能正试图克服多线程问题;在这种情况下,您可以更简单地编写它(并且类型安全),如下所示:

private void buddyAddYouRequest(object sender, string buddy,
        string requestMessage, ref bool bAccept)
{
    bool bAcceptCopy = bAccept;
    this.Invoke(new Action(() => OnBuddyAddYouRequest(sender, buddy,
                                 requestMessage, ref bAcceptCopy)));
    bAccept = bAcceptCopy;
}

// ...

yahoo.OnBuddyAddYouRequest += buddyAddYouRequest;