public todictionary 方法中的字典

public dictionary in todictionary method

我正在尝试访问在其他过程中的 todictionary 方法中创建的字典。

这是我想要的,但我遇到了编译错误。

Public dctOPT As New Dictionary(Of Integer, Object)

 Sub Get_RTD_OPT()

        Dim lstOPT = From C In DATA.CHAINs, O In DATA.OPTIONs, U In DATA.UDLies
                     Where C.CONTRACT = O.CONTRACT And O.UDLY_SYM = U.UDLY_SYM
                     Select C.ID, C.P_C, C.STRIKE, C.CONTRACT,
                        O.OPT_EXCHANGE, O.OPT_EXPIRY, O.OPT_SEC_TYPE, O.SYM,
                         U.CURRENCY

        dctOPT = lstOPT.ToDictionary(Function(Z) Z.ID)

end sub

这可以编译,但是我无法在其他程序中访问 dctOPT

dim dctOPT = lstOPT.ToDictionary(Function(Z) Z.ID)

我需要将 dctOPT 复制到另一个 public 词典中吗?还是有更简单的方法?请问有什么建议吗?

你需要这个:

Public dctOPT As New Dictionary(Of Integer, Object)

Sub Get_RTD_OPT()

    Dim lstOPT =
        From C In DATA.CHAINs, O In DATA.OPTIONs, U In DATA.UDLies
        Where C.CONTRACT = O.CONTRACT And O.UDLY_SYM = U.UDLY_SYM
        Select C.ID, C.P_C, C.STRIKE, C.CONTRACT,
           O.OPT_EXCHANGE, O.OPT_EXPIRY, O.OPT_SEC_TYPE, O.SYM,
            U.CURRENCY

    dctOPT = lstOPT.ToDictionary(Function(Z) Z.ID, Function(Z) CType(Z, Object))

End Sub

如果没有 Function(Z) CType(Z, Object),正在创建的字典类型是 Dictionary(Of Integer, <anonymous>),不能直接分配给 Dictionary(Of Integer, Object)。转换正确地生成了正确类型的字典。