none
Conversion of an array to generic.list RRS feed

  • Question

  • We are currently converting a larger legacy application from .Net1.1 to .Net2.0, both VB. Our new standard is Generic List, instead of arrays.

    We are recoding component by component. And while not everything is yet stored in generic.list we end up with compile time errors.

    Converting from generic.list to array is supported with myGenList.ToArray().
    But from array to generic list isnt that easy.

    "Value of type '1-dimensional array of something' cannot be convertet to 'System.Collection.Generic.List(of something)"

    I know for sertain that the array consist of the type 'something', so an runtime exception converting is ok.

    Is there a good way to do the workaround untill the hole application is recoded?
    Wednesday, November 2, 2005 11:43 AM

Answers



  • Dim someArray() As Integer = ...
    Dim genericList As New List(Of Integer)
    genericList.AddRange(someArray)

     
    Wednesday, November 2, 2005 12:06 PM
    Moderator

All replies



  • Dim someArray() As Integer = ...
    Dim genericList As New List(Of Integer)
    genericList.AddRange(someArray)

     
    Wednesday, November 2, 2005 12:06 PM
    Moderator
  • Thanks, that was the solution Smile
    Wednesday, November 2, 2005 12:14 PM
  • This solves my problem too.. Thanks. Smile
    Friday, November 23, 2007 2:23 AM
  • Me, too. ;)
    Tuesday, March 15, 2011 9:28 PM
  • Hi ALL,

    Here are two other ways:>>

        Dim myIntegerArray() As Integer = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}
    
        Dim myIntegerList1 As List(Of Integer) = myIntegerArray.ToList 'Method1
    
        Dim myIntegerList2 As New List(Of Integer)(myIntegerArray) 'Method2
    
    



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    • Proposed as answer by SP1R1TM4N Tuesday, March 15, 2011 10:00 PM
    Tuesday, March 15, 2011 9:42 PM
  • And this leads nicely to:

     Private MyList As List(Of KeyValuePair(Of String, Integer)) = {New KeyValuePair(Of String, Integer)("can't", 27),
                                     New KeyValuePair(Of String, Integer)("think", 118),
                                     New KeyValuePair(Of String, Integer)("of", 4),
                                     New KeyValuePair(Of String, Integer)("anything", 75),
                                     New KeyValuePair(Of String, Integer)("to", 13),
                                     New KeyValuePair(Of String, Integer)("say", 59)}.ToList
    
    Thanks, John.

    Tuesday, March 15, 2011 9:59 PM
  • Hi SP1R1TM4N,

    Would you not find a Dictionary easier to use? >>

    Dim myD As New Dictionary(Of String, Integer)

    It has a lot more available methods compared to a KeyValuePair .

    :-D

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 12:04 AM
  • Hi John

    Yes, a dictionary would probably be better. My example, though, was just to extend the tips in this thread from create array, assign to list to create list directly from literal array .

    While we're on the subject of lists and arrays, maybe you can see a better way of doing the following:

    I had pairs (Of Integer, String) which I wanted sorting by their integer values. Unfortunately, there was some duplication so I couldn't use a SortedDictionary (needs unique keys). I also needed it to be easily added to, so I wanted a collection type, not an array. In the end, I had two lists, one for the integers and one for the strings. Once I was ready to sort them, I converted them to arrays and used the Array.Sort(Array1, Array2) method. There must be a better way, though.

    Regards

    Alec.

    Wednesday, March 16, 2011 12:38 AM
  • Hi John

    Yes, a dictionary would probably be better. My example, though, was just to extend the tips in this thread from create array, assign to list to create list directly from literal array .

    While we're on the subject of lists and arrays, maybe you can see a better way of doing the following:

    I had pairs (Of Integer, String) which I wanted sorting by their integer values . Unfortunately, there was some duplication so I couldn't use a SortedDictionary (needs unique keys). I also needed it to be easily added to, so I wanted a collection type, not an array. In the end, I had two lists, one for the integers and one for the strings. Once I was ready to sort them, I converted them to arrays and used the Array.Sort(Array1, Array2) method. There must be a better way, though .

    Regards

    Alec.

    Hi Alec,

    Yes of course. Run this, it uses a LINQ query.  :-)

    Please start new questions in a NEW thread in future though, thanks.  :-D

     

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.Collections
    Imports System.Collections.Generic
    
    Public Class Form1
    
      Private MyList As List(Of KeyValuePair(Of String, Integer)) = _
      (New KeyValuePair(Of String, Integer)() _
      {New KeyValuePair(Of String, Integer)("can't", 27), _
       New KeyValuePair(Of String, Integer)("think", 118), _
       New KeyValuePair(Of String, Integer)("of", 4), _
       New KeyValuePair(Of String, Integer)("anything", 75), _
       New KeyValuePair(Of String, Integer)("to", 13), _
       New KeyValuePair(Of String, Integer)("say", 59)}).ToList
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim result As System.Linq.IOrderedEnumerable(Of KeyValuePair(Of String, Integer)) = From kvp As KeyValuePair(Of String, Integer) In MyList Order By kvp.Value
        Dim newList As New List(Of KeyValuePair(Of String, Integer))
        newList = result.ToList
    
        Dim sb As New System.Text.StringBuilder
    
        For index As Integer = 0 To newList.Count - 1
          sb.Append(newList.Item(index).Key & " " & newList.Item(index).Value.ToString & System.Environment.NewLine)
        Next
    
        MessageBox.Show(sb.ToString)
    
      End Sub
    End Class
    



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 1:02 AM
  • Hi John

    Good answer. Thanks for that. I'll be using this method, for sure.

    I'm a little thrown by your syntax in the list assignment, though. I'm guessing you're not using VB2010. My original syntax works perfectly - no line continuation characters necessary, and a {}-bound comma-delimited list of literals already represents an array, i.e. it doesn't need to be preceded by Type().

    Regards,

    Alec.

     

    p.s. Yes, I'll start a new thread in future. ;)

    Wednesday, March 16, 2011 1:21 AM
  • And this leads nicely to:

    Private MyList As List(Of KeyValuePair(Of String, Integer)) = {New KeyValuePair(Of String, Integer)("can't", 27),
             New KeyValuePair(Of String, Integer)("think", 118),
             New KeyValuePair(Of String, Integer)("of", 4),
             New KeyValuePair(Of String, Integer)("anything", 75),
             New KeyValuePair(Of String, Integer)("to", 13),
             New KeyValuePair(Of String, Integer)("say", 59)}.ToList
    

    Thanks, John.

     

    Hi Alec a.k.a. SP1R1TM4N,

    You had the syntax slightly wrong, see my post above.

    Here is another directly assigned example:>>

     

    Dim myIntegerList1 As List(Of Integer) = (New Integer() {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}).ToList

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 1:27 AM
  • John

    Nope, sorry, that is more verbose than it needs to be.

    The following works fine:

    Private test As List(Of Integer) = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}.ToList
    

    :D

    Alec

    Wednesday, March 16, 2011 1:35 AM
  • Hi John

    Good answer. Thanks for that. I'll be using this method, for sure.

    I'm a little thrown by your syntax in the list assignment, though. I'm guessing you're not using VB2010 . My original syntax works perfectly - no line continuation characters necessary, and a {}-bound comma-delimited list of literals already represents an array, i.e. it doesn't need to be preceded by Type().

    Regards,

    Alec.

     

    p.s. Yes, I'll start a new thread in future. ;)

    Hi Alec,

    I will have to try your syntax in VB.Net 2010.  :-)

    I actually use both 2008 and 2010.

    However I find the IDE in the 2008 version seems to work faster and smoother on the

    laptop that I'm using so I use it more often. I use VB.Net 2010 as and when required.

     

    I may get more memory or / and a larger hard-drive for this laptop soon and have the fun of transferring

    my existing setup to a new hard-drive whenever I may feel like splashing out some cash on it.

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 1:36 AM
  • John

    Nope, sorry, that is more verbose than it needs to be.

    The following works fine:

     

    Private test As List(Of Integer) = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}.ToList

     

    :D

    Alec


    Hi Alec,

    I think more people are going to have to start using VB.Net 2010 then.  :-)

    Your above line does not work in 2008 versions.  :-(

    Therefore I tend to write code that I know will work in both 2008 and 2010 versions.  ;-)

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 1:42 AM
  • [...]


    Hi Alec,

    I think more people are going to have to start using VB.Net 2010 then.   :-)

    [...]

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

     

    Yep, looks like it. :P

     

    Alec.

     

    Wednesday, March 16, 2011 1:49 AM
  • Hi Alec,

    Just so you know the following line in my earlier code:>>

    Dim result As System.Linq.IOrderedEnumerable(Of KeyValuePair(Of String, Integer)) = From kvp As KeyValuePair(Of String, Integer) In MyList Order By kvp.Value
    
    

     

    can be changed to:>>

    Dim result As System.Collections.Generic.IEnumerable(Of KeyValuePair(Of String, Integer)) = From kvp As KeyValuePair(Of String, Integer) In MyList Order By kvp.Value
        
    
    and it will still work. :-)



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 1:57 AM
  • That won't work as standalone array literal usage was added in 2010.

    Its just evolution of a language and more features being added.

     

    Wednesday, March 16, 2011 2:05 AM
  • Hi Alec,

    Just so you know the following line in my earlier code:>>

     

    Dim
     result As
     System.Linq.IOrderedEnumerable(Of KeyValuePair(Of String
    , Integer
    )) = From
     kvp As
     KeyValuePair(Of String
    , Integer
    ) In
     MyList Order
     By
     kvp.Value
    
    

     

    can be changed to:>>

     

    Dim
     result As
     System.Collections.Generic.IEnumerable(Of KeyValuePair(Of String
    , Integer
    )) = From
     kvp As
     KeyValuePair(Of String
    , Integer
    ) In
     MyList Order
     By
     kvp.Value
      
    
    and it will still work. :-)

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7


    That's useful. Thanks for letting me know. ;)

    Alec.
    Wednesday, March 16, 2011 2:06 AM
  • What is it with the formatting on this forum?? :(
    Wednesday, March 16, 2011 2:07 AM
  • That won't work as standalone array literal usage was added in 2010.

    Its just evolution of a language and more features being added.

     

    Hi spotty,

    I think I now still prefer the full

    Dim myIntArray() As Integer = New Integer() {1,3,5,7,9,2,4,6,8,10}

    compared to simply

    Dim myIntArray() As Integer = {1,3,5,7,9,2,4,6,8,10}

    as the former is backwards compatible with 2008 when creating a LIST from it.  :-)

     

    Then creating a LIST you can surround the lot with parentheses a.k.a. rounded brackets:>>

    Dim myIntegerList As List(Of Integer) = ( New Integer() {1,3,5,7,9,2,4,6,8,10}  ).ToList

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 2:14 AM
  • This can be done in a number of ways to initialize the collection - and may not require an array at all.

    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
          'With Standalone Array and ToList Method
          Dim MyList As List(Of KeyValuePair(Of String, Integer)) = {New KeyValuePair(Of String, Integer)("can't", 27),
                New KeyValuePair(Of String, Integer)("think", 118),
                New KeyValuePair(Of String, Integer)("of", 4),
                New KeyValuePair(Of String, Integer)("anything", 75),
                New KeyValuePair(Of String, Integer)("to", 13),
                New KeyValuePair(Of String, Integer)("say", 59)}.ToList
    
    
    
          '//Collection Initializer
          Dim MyList1 As New List(Of KeyValuePair(Of String, Integer)) From {New KeyValuePair(Of String, Integer)("can't", 27),
               New KeyValuePair(Of String, Integer)("think", 118),
               New KeyValuePair(Of String, Integer)("of", 4),
               New KeyValuePair(Of String, Integer)("anything", 75),
               New KeyValuePair(Of String, Integer)("to", 13),
               New KeyValuePair(Of String, Integer)("say", 59)}
    
    
    
          '//Collection Initializer with Generic Extension Method
          Dim MyList2 As New List(Of KeyValuePair(Of String, Integer)) From {{"can't", 27},
                           {"think", 118},
                           {"of", 4},
                           {"anything", 75},
                           {"to", 13},
                           {"say", 59}}
    
    
    
    
          '//Collection Initializer 
          Dim Dictionary As New Dictionary(Of String, Integer) From {{"can't", 27},
                                            {"think", 118},
                                            {"of", 4},
                                            {"anything", 75},
                                            {"to", 13},
                                            {"say", 59}}
    
    
    
    
    
        Catch ex As Exception
          MsgBox(ex.Message)
        End Try
      End Sub
    End Class
    
    Module ExtensionMethods
      <Runtime.CompilerServices.Extension()> Sub Add(Of t, u)(ByVal x As List(Of KeyValuePair(Of t, u)), ByVal x1 As t, ByVal y1 As u)
        x.Add(New KeyValuePair(Of t, u)(x1, y1))
      End Sub
    
    End Module
    
    
    
    
    
    
    Wednesday, March 16, 2011 2:16 AM
  • What is it with the formatting on this forum?? :(


    Hi Alec,

    I know.

    I don't know if it is to do with the browser some of us may be using but when you quote a post with code it goes haywire!!



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 2:23 AM
  • You simply talking about the IDE

    The code is completely backward compatible with all versions of the framework from 2010 backward.

    If your populating a list you doing the

    Dim myIntegerList As List(Of Integer) = ( New Integer() {1,3,5,7,9,2,4,6,8,10}  ).ToList

    is more inefficient than simply using a collection intializer

    Dim myIntegerList As New List(Of Integer) FROM {1,3,5,7,9,2,4,6,8,10} 

    Collection and standalone array initializers are implemented to save having to write extra code.   

     

    Wednesday, March 16, 2011 2:24 AM
  • That won't work as standalone array literal usage was added in 2010.

    Its just evolution of a language and more features being added.

     

    Hi spotty,

    I think I now still prefer the full

    Dim myIntArray() As Integer = New Integer() {1,3,5,7,9,2,4,6,8,10}

    compared to simply

    Dim myIntArray() As Integer = {1,3,5,7,9,2,4,6,8,10}

    as the former is backwards compatible with 2008 when creating a LIST from it.  :-)

     

    Then creating a LIST you can surround the lot with parentheses a.k.a. rounded brackets:>>

    Dim myIntegerList As List(Of Integer) = ( New Integer() {1,3,5,7,9,2,4,6,8,10}  ).ToList

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    And with Option Infer On, it's just Dim myIntArray() = {1,3,5,7,9,2,4,6,8,10} ! lol

     

    (I don't use it though - I think it encourages sloppy programming.)

     

    Alec ;)

    Wednesday, March 16, 2011 2:29 AM
  • What is it with the formatting on this forum?? :(


    Hi Alec,

    I know.

    I don't know if it is to do with the browser some of us may be using nut when you quote a post with code it goes haywire!!



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    I think it's a deliberate ploy by MS, cos I'm using FireFox, not IE. :P
    Wednesday, March 16, 2011 2:29 AM
  • This can be done in a number of ways to initialize the collection - and may not require an array at all.

    [...]
    
    '//Collection Initializer
    Dim Dictionary As New Dictionary(Of String , Integer ) From {{"can't" , 27},
    {"think" , 118},
    {"of" , 4},
    {"anything" , 75},
    {"to" , 13},
    {"say" , 59}}


    [...]

    That's very neat. I like that. I had previously overlooked the From keyword, for some reason.

    Regards,

    Alec

    Wednesday, March 16, 2011 2:32 AM

  • I think it's a deliberate ploy by MS, cos I'm using FireFox, not IE. :P

    Probably!!

    And with Option Infer On, it's just Dim myIntArray() = {1,3,5,7,9,2,4,6,8,10} ! lol

     

    (I don't use it though - I think it encourages sloppy programming.)

     

    Alec ;)

    Agreed!!

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 2:34 AM
  • You simply talking about the IDE

    The code is completely backward compatible with all versions of the framework from 2010 backward.

    If your populating a list you doing the

    Dim myIntegerList As List(Of Integer) = ( New Integer() {1,3,5,7,9,2,4,6,8,10}  ).ToList

    is more inefficient than simply using a collection intializer

    Dim myIntegerList As New List(Of Integer) FROM {1,3,5,7,9,2,4,6,8,10} 

    Collection and standalone array initializers are implemented to save having to write extra code.   

     


    Hi spotty,

    Neither of these will work in Vb.Net 2008 of course.>>

    Additionally .ToList is an extension method and they were introduced with VB.Net 2008 I believe.

        Dim myIntegerList1 As New List(Of Integer) From {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}
        Dim myIntegerList2 As List(Of Integer) = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}.ToList
    




    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 3:02 AM
  • You simply talking about the IDE

    The code is completely backward compatible with all versions of the framework from 2010 backward.

     

    Hi spotty,

    Sorry, I disagree.

     

    Try targeting any Framework earlier than 3.5 in the 2010 IDE then.

    You will see this does not work:>>

    Dim myIntegerList2 As List(Of Integer) = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}.ToList



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 3:21 AM
  • You simply talking about the IDE

    The code is completely backward compatible with all versions of the framework from 2010 backward.

     

    Hi spotty,

    Sorry, I disagree.

     

    Try targeting any Framework earlier than 3.5 in the 2010 IDE then.

    You will see this does not work:>>

    Dim myIntegerList2 As List(Of Integer) = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}.ToList



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7


    I'm sure multiple declarations with assignments like this can't be done in earlier versions:

    Dim i As New Info(False), sc() As Char = {"*"c}, f() As String = s.Split("/")
    

    This works in 2010! :D

    Alec

    Wednesday, March 16, 2011 3:40 AM
  • Hi Alec,

    I can't quickly verify it one way or another but as far as I know that has been possible with all versions of VB.Net.

    It is certainly possible with 2008 and 2010 but I don't have a computer with 2003 or 2005 installed.

    I've never owned VB.Net 2002 ( any version ).



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 3:54 AM
  • Hi Alec,

    I can't quickly verify it one way or another but as far as I know that has been possible with all versions of VB.Net.

    It is certainly possible with 2008 and 2010 but I don't have a computer with 2003 or 2005 installed.

    I've never owned VB.Net 2002 ( any version ).



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7


    Ignore me, I'm being a moron! I was confusing it with assigning values to multiple, comma-delimited vars declared with a single type (like a, b As Integer = 5).

    (It is 4am here, after all. I'd better go to bed, I suppose.)

    Alec

    Wednesday, March 16, 2011 4:03 AM
  • Ignore me, I'm being a moron! I was confusing it with assigning values to multiple, comma-delimited vars declared with a single type (like a, b As Integer = 5).

    (It is 4am here , after all. I'd better go to bed, I suppose.)

    Alec

    Hi Alec,

    Are you in the U.K. too?



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 4:11 AM
  • Ignore me, I'm being a moron! I was confusing it with assigning values to multiple, comma-delimited vars declared with a single type (like a, b As Integer = 5).

    (It is 4am here , after all. I'd better go to bed, I suppose.)

    Alec

    Hi Alec,

    Are you in the U.K. too?



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7


    Yup, Kent.

    (Why have I still not shut down and gone to bed??) :|

    Nite.

    Alec

    Wednesday, March 16, 2011 4:13 AM
  • Yup, Kent.

    (Why have I still not shut down and gone to bed??) :|

    Nite.

    Alec

    Hi Alec,

    A Friend of mine used to work in Medway for a while. He is currently back home up here in Middlesbrough, Cleveland.

    Good night & good morning!! LOL!!    :-D



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 4:16 AM
  • Apologies, 3.5 targeting will work just fine as it uses ToList.   It is possible to hack an extension method for 2.0 targets to implement the  ToList function

    To make it work with a 2010 IDE targeting 3.5, just add the following imports and it will work quite happily.

    Imports

     

     

    System.Linq

     

    Wednesday, March 16, 2011 5:32 AM
  • Apologies, 3.5 targeting will work just fine as it uses ToList.   It is possible to hack an extension method for 2.0 targets to implement the  ToList function

    To make it work with a 2010 IDE targeting 3.5, just add the following imports and it will work quite happily.

     

     

    Hi spotty,

    I'd be interested in seeing code you may have in mind to achieve that please

    or would this simply be a standard Function that creates a List from an array?



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Wednesday, March 16, 2011 2:05 PM
  • The method is nothing special really - to create the extension method for 2.0 targets you need to create an extension attribute (it needs to reside in exactly the same namespace as the one which normally is in system.core in 3.5 with the same signature).    This can reside in you own assembly and then you extension method would be extending type an Array Type returning a list type - using generics is probably a good bet.

    The content of the extension method is simply iterating through array members and adding to list.

     

    Wednesday, March 16, 2011 3:13 PM
  • The method is nothing special really - to create the extension method for 2.0 targets you need to create an extension attribute (it needs to reside in exactly the same namespace as the one which normally is in system.core in 3.5 with the same signature).    This can reside in you own assembly and then you extension method would be extending type an Array Type returning a list type - using generics is probably a good bet.

    The content of the extension method is simply iterating through array members and adding to list.

     


    Hi spotty,

    Are you thinking of something like the following code and then targetting Framework 2.0 ?

    If so the minimum Framework for an extension method is Framework 3.5

     

    Module MyExtensions
    
     <System.Runtime.CompilerServices.Extension()> _
     Public Function ToList(Of T)(ByVal anArray() As T) As List(Of T)
    
      Dim returnList As New List(Of T)
      returnList.AddRange(anArray)
      Return returnList
    
     End Function
    
    End Module
    

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Thursday, March 17, 2011 10:58 PM
  • It is in your example but if you read my article

    http://msdn.microsoft.com/en-us/magazine/cc163317.aspx

    It will tell you how you can use extension methods to target 2.0 framework applications,

    It is blocked by default as this attribute existing in system.core which does not ship with 2.0 framework but when using 2008 to target 2.0 you are still using the 3.5 compiler to build the binaries and it knows what extension attribute means.

    Thursday, March 17, 2011 11:16 PM
  • It is in your example but if you read my article

    http://msdn.microsoft.com/en-us/magazine/cc163317.aspx

    It will tell you how you can use extension methods to target 2.0 framework applications,

    It is blocked by default as this attribute existing in system.core which does not ship with 2.0 framework but when using 2008 to target 2.0 you are still using the 3.5 compiler to build the binaries and it knows what extension attribute means.


    Hi spotty,

    That is the best tip I've seen on these forums for a long time. Many thanks for that one.  :-)    :-D

    By the way, how long have you been working at Redmond now?

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Friday, March 18, 2011 12:26 AM
  • I've been working on Compiler in Redmond for 6 years now.

    Extension Methods, Collection and Array Initializers are two of the features that I've worked on in last couple of product cycles.   So I know a few of quite a bit about these.

    Friday, March 18, 2011 4:39 PM
  • I've been working on Compiler in Redmond for 6 years now.

    Extension Methods, Collection and Array Initializers are two of the features that I've worked on in last couple of product cycles.   So I know a few of quite a bit about these.

    Hi again spotty,

    It is good to know what certain individuals have been working on I feel. :-D

    Keep up the good work.  :-)



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Friday, March 18, 2011 10:16 PM