简体   繁体   中英

Need help converting this code snippet from c# to vb

Hey i've tried all the online code converters but none of them work on this. Can someone please explain to me how to transform this sniped it to 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];
};

Change the inner code to a separate method:

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];
}

and then change the line you quoted into this:

yahoo.OnBuddyAddYouRequest += buddyAddYouRequest;

Then you can run this through a code converter to convert it to VB.NET.

However, I should point out that the code makes no sense. It seems to invoke an existing method, OnBuddyAddYouRequest , but in an unnecessarily roundabout way. I realise that the author may be trying to overcome multi-threading issues; in that case, you can write it much more simply (and type-safe) like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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