Question

Background: I wrote code that will output all the currently logged in User's Active Directory group names. I want the group name (ex: Acomp_user_BIG) via IdentityReference.Translate instead of the current user's SID's (ex: S-1-5-32-544) returned via IdentityReference.Value.

Here is code that I used:

Public ReadOnly Property Groups As IdentityReferenceCollection
    Get
         Dim irc As IdentityReferenceCollection
         Dim ir As IdentityReference
         irc = WindowsIdentity.GetCurrent().Groups

         For Each ir In irc
              Dim account As IdentityReference = ir.Translate(GetType(NTAccount))
              Debug.WriteLine(account.Value)
         Next

    End Get
End Property

Unfortunately, I get the error on the "End Get":

   Warning  1   Property 'Groups' doesn't return a value on all code paths. 
                A null reference exception could occur at run time when the 
                result is used. 

Any suggestions for fixing this error?

Was it helpful?

Solution

Public ReadOnly Property Groups As IdentityReferenceCollection
    Get
         Dim irc As IdentityReferenceCollection
         Dim ir As IdentityReference
         irc = WindowsIdentity.GetCurrent().Groups

         For Each ir In irc
              Dim account As IdentityReference = ir.Translate(GetType(NTAccount))
              Debug.WriteLine(account.Value)
         Next

         **return irc**
    End Get
End Property

However by looking at the code the irc gets modified so you should make a new IdentityReferenceCollection (assuming it has a .add() method) then do .add(account) and return your new collection

OTHER TIPS

Your code doesn't return anything in any of the code paths.

I'm not sure what you want it to return, but returning the collection would match the type of the property:

Return irc

That would however make most of the code in the property unnecessary, so perhaps you want a different return type, or maybe you don't want to make a property at all.

A Get property should return a value. Your property doesn’t do that; you are merely printing some things to the console. This isn’t what a property is there for.

A Get property should Return some value, and shouldn’t do much else besides. If you want to perform some logic with side-effects, use a Sub. If you also want to return a value, use a Function.

Only use properties if you merely want to return some value without having a computation with side-effects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top