如何从函数返回字典?

发布于 2024-09-29 01:37:13 字数 396 浏览 0 评论 0原文

这对我不起作用,目前还不清楚为什么。

Sub mySub()
    dim myDict as Dictionary
    myDict=new Dictionary
            
    myDict=myFunc()
                
End Sub
        
Function myFunc()
    dim myDict2
    set myDict2 = new Dictionary
                    
    'some code that does things and adds to myDict2'
                    
    myFunc=myDict2
End Function

如何从函数返回字典?

This is not working for me, and it is unclear why.

Sub mySub()
    dim myDict as Dictionary
    myDict=new Dictionary
            
    myDict=myFunc()
                
End Sub
        
Function myFunc()
    dim myDict2
    set myDict2 = new Dictionary
                    
    'some code that does things and adds to myDict2'
                    
    myFunc=myDict2
End Function

How can I return a dictionary from a function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

(り薆情海 2024-10-06 01:37:13

每当您分配对象而不是时,您都需要使用SET关键字:

    Sub mySub()
        dim myDict as Dictionary
        set myDict = myFunc()
    End Sub

    Function myFunc() as Dictionary
        dim myDict2 as Dictionary
        set myDict2 = new Dictionary
                'some code that does things and adds to myDict2'
        set myFunc=myDict2
    End Function

您的原始代码还将myDict创建为新的Dictionary对象,然后立即替换与另一个不同的。您可以跳过该步骤。

You'll need to use the SET keyword anytime you are assigning an object instead of a value:

    Sub mySub()
        dim myDict as Dictionary
        set myDict = myFunc()
    End Sub

    Function myFunc() as Dictionary
        dim myDict2 as Dictionary
        set myDict2 = new Dictionary
                'some code that does things and adds to myDict2'
        set myFunc=myDict2
    End Function

Your original code was also creating myDict as a new Dictionary object, then immediately replacing it with a different one. You can just skip that step.

×纯※雪 2024-10-06 01:37:13

我发现这是一个老问题,但是帖子和解决方案帮助我解决了这个问题,并且我将其提升到了一个新的水平。这是一个小小的飞跃。谢谢你!

如何转换用进程名称和 ID 填充字典的函数,以便它返回字典对象?然后,用字典内容填充工作表是一个简单的任务,这是我从博客中学到的。我希望我有作者的名字,但包含了链接。

当然假设 Sheet1。随心所欲地定制。这与你们两人发布的内容相比,又是一个小小的飞跃。绝对出色的工作,谢谢你们!

Sub Test_AllRunningApps()
    Dim apps As Dictionary
    Set apps = AllRunningApps()

    'Populate a sheet with a dictionary - http://exceldevelopmentplatform.blogspot.com/2018/05/vba-writing-dictionaries-to-worksheet.html
    Sheet1.Cells(1, 1).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Keys)
    Sheet1.Cells(1, 2).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Items)

    Set apps = Nothing
End Sub

'Similar to: http://msdn.microsoft.com/en-us/library/aa393618%28VS.85%29.aspx
Public Function AllRunningApps() As Dictionary
    Dim strComputer As String
    Dim objServices As Object, objProcessSet As Object, Process As Object
    Dim oDic As Object, oDic2 As Object, a() As Variant

    Set oDic = CreateObject("Scripting.Dictionary")

    strComputer = "."

    Set objServices = GetObject("winmgmts:\\" _
        & strComputer & "\root\CIMV2")
    Set objProcessSet = objServices.ExecQuery _
        ("Select Name, ProcessID FROM Win32_Process", , 48)

    For Each Process In objProcessSet
       If Not oDic.exists(Process.Name) Then
        oDic.Add Key:=Process.Properties_("Name").Value, Item:=Process.Properties_("ProcessID").Value
       End If
    Next

    Set AllRunningApps = oDic

    Set objProcessSet = Nothing
    Set oDic = Nothing
End Function

I see that this is an old question, but the post AND solution helped me figure this out, and I took it to the next level. It was a small leap. Thank you!

How about converting your function that populates the dictionary with process names and IDs so it returns a dictionary object? Then it is a simple task of populating a sheet with the dictionary contents, which I learned to do from a blog. I wish I had the author's name but the link is included.

Sheet1 was assumed of course. Customize however you wish. Again this was a small leap from what both of you posted. Absolutely brilliant work guys, thank you!

Sub Test_AllRunningApps()
    Dim apps As Dictionary
    Set apps = AllRunningApps()

    'Populate a sheet with a dictionary - http://exceldevelopmentplatform.blogspot.com/2018/05/vba-writing-dictionaries-to-worksheet.html
    Sheet1.Cells(1, 1).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Keys)
    Sheet1.Cells(1, 2).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Items)

    Set apps = Nothing
End Sub

'Similar to: http://msdn.microsoft.com/en-us/library/aa393618%28VS.85%29.aspx
Public Function AllRunningApps() As Dictionary
    Dim strComputer As String
    Dim objServices As Object, objProcessSet As Object, Process As Object
    Dim oDic As Object, oDic2 As Object, a() As Variant

    Set oDic = CreateObject("Scripting.Dictionary")

    strComputer = "."

    Set objServices = GetObject("winmgmts:\\" _
        & strComputer & "\root\CIMV2")
    Set objProcessSet = objServices.ExecQuery _
        ("Select Name, ProcessID FROM Win32_Process", , 48)

    For Each Process In objProcessSet
       If Not oDic.exists(Process.Name) Then
        oDic.Add Key:=Process.Properties_("Name").Value, Item:=Process.Properties_("ProcessID").Value
       End If
    Next

    Set AllRunningApps = oDic

    Set objProcessSet = Nothing
    Set oDic = Nothing
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文