-3

Hi Was wondering wheher anyone could shed some light on translating the below code to vb.net. Any help will be greatly appreciated.

void ShowNewPage()
{
    Dispatcher.Invoke((Action)delegate 
    {
        if (contentPresenter.Content != null)
        {
            UserControl oldPage = contentPresenter.Content as UserControl;

            if (oldPage != null)
            {
                oldPage.Loaded -= newPage_Loaded;

                UnloadPage(oldPage);
            }
        }
        else
        {
            ShowNextPage();
        }

    });
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 3
    You could try searching your title in google – Jeff Mar 27 '14 at 15:44
  • Thansk but I did try the translation sites and got the same but I get an error saying "Single line statement lambdas must include exactly one statement" – user3469277 Mar 27 '14 at 15:54
  • 3
    So ask a question about *that error*... show your code, show what you've tried and explain the error. You will get better answers that way. Don't just ask people to "do it for me". – Evan L Mar 27 '14 at 15:54
  • 1
    Why shouldn't he just ask people to do it for him? He might get downvoted and people might complain, but hey look below, TWO people have already provided him the translation. Good luck expecting a user with 1 rep to care about being downvoted! The only way you can prevent this is to downvote the people who provide answers.... – Weyland Yutani Mar 27 '14 at 15:57
  • Apologies i thought that by posting the original code it would help as perhaps the translators havent worked correctly. – user3469277 Mar 27 '14 at 15:58
  • @WeylandYutani I try to give people the benefit of the doubt. The OP had every intention of trying to solve his problem, he/she just didn't know what the problem was until we dug it out of him. Which never would have happened if TWO people hadn't gone out and tried to help. If the OP just wants people to do his work for him... then in this case he got what he wanted, oh well, no sweat off my back or yours. So tell me *why* in the world would you downvote someone just to stop them from posting translations? It wont work, and just further propagates elitist egos. – Evan L Mar 27 '14 at 16:13
  • but what if i want to be a dick? – Weyland Yutani Mar 27 '14 at 16:19
  • 1
    @WeylandYutani Then you have succeeded ;) – Evan L Mar 27 '14 at 16:20
  • :D i didn't downvote btw. i rarely downvote, i don't care. I more often upvote negative questions. – Weyland Yutani Mar 27 '14 at 16:21
  • Very important question--which version of Visual Studio are you using? Multi-line lambdas are only available in VS2010+. Your other option is to define a separate `Sub` and have the Lambda call it, which would make it one line. – j.i.h. Mar 27 '14 at 16:30

2 Answers2

2

Quick search on google for: translate C# to vb.net will get you on your way ;)

But for Answer's sake, here you go!

UPDATED:

Private Sub ShowNewPage()
    Dispatcher.Invoke(DirectCast(
                          Sub()
                              If contentPresenter.Content IsNot Nothing Then
                                  Dim oldPage As UserControl = TryCast(contentPresenter.Content, UserControl)

                                  If oldPage IsNot Nothing Then
                                      oldPage.Loaded -= newPage_Loaded
                                      UnloadPage(oldPage)
                                  End If
                              Else
                                  ShowNextPage()
                              End If
                          End Sub, 
                          Action))
End Sub
Dayan
  • 7,634
  • 11
  • 49
  • 76
  • Lol we must have used the same translator ;) – Evan L Mar 27 '14 at 15:49
  • 1
    @EvanL Indeed! +1 http://www.developerfusion.com/tools/convert/csharp-to-vb/?batchId=69fa852d-d795-48f1-b5b9-f0cfc5a51a54 ;) – Dayan Mar 27 '14 at 15:49
  • Actually I used [Telerik's Converter](http://converter.telerik.com/) but this snippet came out identical, guess there aren't too many ways to write this particular block ;) – Evan L Mar 27 '14 at 15:51
  • @EvanL Interesting, maybe that web app is using Telerik backend :P – Dayan Mar 27 '14 at 15:54
  • Thanks but I did try the translation sites and got the same but I get an error saying "Single line statement lambdas must include exactly one statement" – user3469277 Mar 27 '14 at 15:58
  • The single-line statement error is from the fact that you need a matching `End Sub` for multi-line lambda `Sub`s, and the opening `Sub()` must be on its own line. – j.i.h. Mar 27 '14 at 16:49
1

Online translators do not always work. They are particularly bad at linq and lambdas.

Here is my attempt at the translation:

Private Sub ShowNewPage()
    Dispatcher.Invoke(Sub() ShowNewPageCallback())
End Sub

Private Sub ShowNewPageCallback()
    If contentPresenter.Content IsNot Nothing Then
        Dim oldPage As UserControl = TryCast(contentPresenter.Content, UserControl)

        If oldPage IsNot Nothing Then
            RemoveHandler oldPage.Loaded, AddressOf newPage_Loaded
            UnloadPage(oldPage)
        End If
    Else
        ShowNextPage()
    End If
End Sub

For future reference if you decide to post a question like this in the future you should mention that you have tried to translate it and show your translation. Most people like to see that you have made some effort in doing the work yourself.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143