none
Select Class Value to Array in Sorted Dictionary RRS feed

  • Question

  • I have a SortedDictionary(Of Integer, PLCBit) where PLCBit is a class;

    Private PLCBits As New SortedDictionary(Of Integer, PLCBit) 

    Public Class PLCBit
        Public Property TagId As Integer
        Public Property TagDesc As String
        Public Property Value As Boolean
    End Class

    After I add a number of PLCBits to the dictionary I need to retrieve an array of Values from the class. The function looks like this;

      Public Function GetBitValueArray() As Boolean()
    
        Dim BitValueArray() As Boolean
    
        Try
          BitValueArray = PLCBits.Values.Select(???)
        Catch ex As Exception
    
        End Try
    
        Return BitValueArray()
    
      End Function
    So I'm stuck on the Select function and not quite sure where to go with it or perhaps an alternate method to extract an array. I'd appreciate a hand here.


    • Edited by Peak Creek Thursday, January 24, 2019 4:28 PM
    Thursday, January 24, 2019 4:17 PM

Answers

  • Try this

    Dim BitValueArray() As Boolean =
            PLCBits.Select(Function(item) item.Value.Value).
            ToArray()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Peak Creek Thursday, January 24, 2019 7:35 PM
    Thursday, January 24, 2019 7:19 PM
    Moderator

All replies

  • This is a template xxxx is your class

    BitValueArray = PLCBits.Values.Select(Function(item) xxxx)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 24, 2019 5:02 PM
    Moderator
  • I changed the name of the PLCBit property "Value" to "BitValue" per the suggestion of leshey.

      Public Function GetBitValueArray() As Boolean()
    
        Dim BitValueArray() As Boolean
    
        Try
          BitValueArray = PLCBits.Values.Select(Function(BitValue) PLCBits)
        Catch ex As Exception
    
        End Try
    
        Return BitValueArray
    
      End Function
    Generates exception: Unable to cast object of type 'WhereSelectEnumerableIterator`2[TestApp.TestClass+PLCBit,System.Collections.Generic.SortedDictionary`2[System.Int32,TestApp.TestClass+PLCBit]]' to type 'System.Boolean[]'.

    Thursday, January 24, 2019 6:08 PM
  • Try this

    Dim BitValueArray() As Boolean =
            PLCBits.Select(Function(item) item.Value.Value).
            ToArray()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Peak Creek Thursday, January 24, 2019 7:35 PM
    Thursday, January 24, 2019 7:19 PM
    Moderator
  • Awesome! Thank you so much Karen! I really suck at Linq. This was very helpful.
    Thursday, January 24, 2019 7:36 PM
  • Awesome! Thank you so much Karen! I really suck at Linq. This was very helpful.

    Don't think of yourself sucking at LINQ but instead in "learning mode", we have all been in your shoes :-)

    Perhaps this might be helpful to learn.

    https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 24, 2019 7:41 PM
    Moderator