Anyone help with the VB.Net implementation of PTPrincipal?

Anyone help with the VB.Net implementation of PTPrincipal?

Old forum URL: forums.lhotka.net/forums/t/11177.aspx


gajit posted on Monday, February 20, 2012

I'm trying to implement the PTPrincipal class in VB.Net, and the direct translation of the C# class produces an error...

Anyone know what the correct syntax should be?

Here's what I get in VB.Net :

Public Shared Sub BeginLogin(username As String, password As String)
            PTIdentity.GetPTIdentity(username, password, Function(o, e)
                                                             If e.[Error] Is Nothing AndAlso e.[Object] IsNot Nothing Then
                                                                 SetPrincipal(e.[Object])
                                                             Else
                                                                 Logout()
                                                             End If
                                                         End Function) '------ doesn't like THIS function aspect.
        End Sub

when I translate THIS code from C#:

    public static void BeginLogin(string username, string password)
    {
      PTIdentity.GetPTIdentity(username, password, (o, e) =>
        {
          if (e.Error == null && e.Object != null)
            SetPrincipal(e.Object);
          else
            Logout();
        });
    }

The design-time error (warning) is:

Function '<anonymous method>' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

I suspect this is the translator implementing the function incorrectly, but I'm not familiar with the syntax.

Thanks,

Graham

 

tmg4340 replied on Monday, February 20, 2012

I don't know a lot about lambdas in VB.NET, but I'm guessing the issue is that the lambda expression is being defined as a function, but the expression doesn't return anything.  So my best suggestion is to replace "Function(o, e)"  to "Sub(o, e)" (and the corresponding "End" tag too).

HTH

- Scott

gajit replied on Monday, February 20, 2012

Hmmm... a Sub seems to correct the error...   I wonder if there's any impact....

Thanks Scott.

RockfordLhotka replied on Monday, February 20, 2012

I'm pretty sure the C# code method is void, so then Sub would be the correct translation.

menzel2911 replied on Wednesday, February 22, 2012

VB.Net looks like this:

Public Shared Sub BeginLogin(ByVal username As String, ByVal password As String)
            CustomIdentity.GetIdentity(username, password, Sub(o, e)
                                                               If e.Error Is Nothing AndAlso e.Object IsNot Nothing Then
                                                                   SetPrincipal(e.Object)
                                                               Else
                                                                   Logout()
                                                               End If
                                                           End Sub)
        End Sub

Copyright (c) Marimer LLC