Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET错误-使用结果时,可能会在运行时发生空引用异常_Vb.net_Error Handling_Compiler Errors_Windows Identity_Usergroups - Fatal编程技术网

VB.NET错误-使用结果时,可能会在运行时发生空引用异常

VB.NET错误-使用结果时,可能会在运行时发生空引用异常,vb.net,error-handling,compiler-errors,windows-identity,usergroups,Vb.net,Error Handling,Compiler Errors,Windows Identity,Usergroups,背景:我编写的代码将输出当前登录用户的所有Active Directory组名。我希望通过IdentityReference.Translate返回组名(例如:Acomp_user_BIG),而不是通过IdentityReference.Value返回当前用户的SID(例如:s-1-5-32-544) 以下是我使用的代码: Public ReadOnly Property Groups As IdentityReferenceCollection Get Dim irc

背景:我编写的代码将输出当前登录用户的所有Active Directory组名。我希望通过IdentityReference.Translate返回组名(例如:Acomp_user_BIG),而不是通过IdentityReference.Value返回当前用户的SID(例如:s-1-5-32-544)

以下是我使用的代码:

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
不幸的是,我在“End get”中得到了错误:


有没有修复此错误的建议?

您的代码不会在任何代码路径中返回任何内容

我不确定您希望它返回什么,但返回集合将匹配属性的类型:

Return irc

但是,这会使属性中的大部分代码变得不必要,因此可能需要不同的返回类型,或者根本不想创建属性。

您的代码不会在任何代码路径中返回任何内容

我不确定您希望它返回什么,但返回集合将匹配属性的类型:

Return irc

但是,这会使属性中的大部分代码变得不必要,因此可能需要不同的返回类型,或者根本不想创建属性。

a
Get
property应该返回值。你的财产不会这样做;您只是将一些内容打印到控制台。这不是房产的用途


一个
Get
属性应该
返回一些值,除此之外不应该做很多其他事情。如果您想执行一些具有副作用的逻辑,请使用
子功能
。如果还想返回值,请使用
函数


仅当您只想返回某些值而不进行有副作用的计算时,才使用属性。

a
Get
属性应返回值。你的财产不会这样做;您只是将一些内容打印到控制台。这不是房产的用途

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

一个
Get
属性应该
返回一些值,除此之外不应该做很多其他事情。如果您想执行一些具有副作用的逻辑,请使用
子功能
。如果还想返回值,请使用
函数

仅当您只想返回一些值而不进行有副作用的计算时才使用属性

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
但是,通过查看代码,irc会被修改,因此您应该创建一个新的IdentityReferenceCollection(假设它有一个.add()方法),然后do.add(account) 并归还您的新收藏

但是,通过查看代码,irc会被修改,因此您应该创建一个新的IdentityReferenceCollection(假设它有一个.add()方法),然后do.add(account)
并退回您的新收藏

谢谢您的回复。但添加“returnIRC”并没有消除这个错误。还有其他建议吗?我是否应该完全摆脱get和end get?如果在“end get”之前的行中添加了“Return irc”,那么您就不会收到相同的错误消息…哦,我的错,我已经将它放在For循环中了。一旦我把它放在“Next”之后,错误就消失了:)我在System.Diagnostics.Debug.listeners集合中的何处/如何检查跟踪侦听器?我正在尝试查看为active directory组返回的值。我添加了一个MsgBox(account.Value),但没有出现任何MsgBox…感谢您的回复。但添加“returnIRC”并没有消除这个错误。还有其他建议吗?我是否应该完全摆脱get和end get?如果在“end get”之前的行中添加了“Return irc”,那么您就不会收到相同的错误消息…哦,我的错,我已经将它放在For循环中了。一旦我把它放在“Next”之后,错误就消失了:)我在System.Diagnostics.Debug.listeners集合中的何处/如何检查跟踪侦听器?我正在尝试查看为active directory组返回的值。我添加了一个MsgBox(account.Value),但没有出现任何MsgBox…感谢您的回复。你推荐什么最好?我是否可以完全删除get和end get?@Brian如果不需要返回值,请删除属性并使用
Sub
。我在System.Diagnostics.Debug.listeners集合中的何处/如何检查跟踪侦听器?我正在尝试查看为active directory组返回的值。我添加了一个MsgBox(account.Value),但没有出现任何MsgBox…感谢您的回复。你推荐什么最好?我是否可以完全删除get和end get?@Brian如果不需要返回值,请删除属性并使用
Sub
。我在System.Diagnostics.Debug.listeners集合中的何处/如何检查跟踪侦听器?我正在尝试查看为active directory组返回的值。我添加了一个MsgBox(account.Value),但没有出现任何MsgBox…我在System.Diagnostics.Debug.listeners集合中的何处/如何检查跟踪侦听器?我正在尝试查看为active directory组返回的值。我添加了一个MsgBox(account.Value),但没有MsgBox出现up@Brian,您应该编写一个单元测试来检查某个对象是否返回正确的值。在System.Diagnostics.Debug.listeners集合中,我在何处/如何检查跟踪侦听器?我正在尝试查看为active directory组返回的值。我添加了一个MsgBox(account.Value),但没有MsgBox出现up@Brian,您应该编写一个单元测试来检查某个对象是否返回正确的值。