0

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?

Brian McCarthy
  • 4,658
  • 16
  • 49
  • 66

3 Answers3

2

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • thanks for your response. Adding "Return irc" Didn't get rid of the the error though. Any other suggestions? Should i just get rid of the get and end get entirely? – Brian McCarthy Apr 28 '11 at 15:59
  • 1
    If you added "Return irc" on the line before "End Get", then you shouldn't get the same error message... – Meta-Knight Apr 28 '11 at 16:11
  • Ohhh my bad, I had put it inside of the For Loop. Once i put it after the "Next" the error disappeared :) – Brian McCarthy Apr 28 '11 at 16:23
  • Where/How do i check the trace listeners in the System.Diagnostics.Debug.Listeners collection? I am trying to see what values are returned for the active directory groups. I added a MsgBox(account.Value) and no msgBox comes up... – Brian McCarthy Apr 28 '11 at 16:29
2

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks for your response. What do you recommend would be the best? Can Should i just get rid of the get and end get entirely? – Brian McCarthy Apr 28 '11 at 16:01
  • 1
    @Brian If you don’t need the return value, get rid of the property and use a `Sub` instead. – Konrad Rudolph Apr 28 '11 at 16:05
  • Where/How do i check the trace listeners in the System.Diagnostics.Debug.Listeners collection? I am trying to see what values are returned for the active directory groups. I added a MsgBox(account.Value) and no msgBox comes up... – Brian McCarthy Apr 28 '11 at 16:29
1
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

David
  • 4,185
  • 2
  • 27
  • 46
  • Where/How do i check the trace listeners in the System.Diagnostics.Debug.Listeners collection? I am trying to see what values are returned for the active directory groups. I added a MsgBox(account.Value) and no msgBox comes up – Brian McCarthy Apr 28 '11 at 18:15
  • @Brian, you should write a unit-test to check if something returns the right value. – David Apr 28 '11 at 19:40