locked
LINQ and Option Strict when using custom object RRS feed

  • Question

  • I use the VS10 express to compile the example in help at the below adress ...

    http://127.0.0.1:47873/help/1-6644/ms.help?method=page&id=C318B79A-FA4D-4DE3-B62D-C1162BEB267E&product=VS&productVersion=100&topicVersion=100&locale=EN-US&topicLocale=EN-US

    and i get the following error
    Error 1 Option Strict On disallows implicit conversions from 'Object' to 'ConsoleApplication1.Module1.Student'. C:\Users\LSB\AppData\Local\Temporary Projects\ConsoleApplication1\Module1.vb 34 80 ConsoleApplication1

    Any ideas?

    Here is the code of the MS console example...


    Imports System.Collections
    Imports System.Linq

    Module Module1

        Public Class Student
            Public Property FirstName As String
            Public Property LastName As String
            Public Property Scores As Integer()
        End Class

        Sub Main()

            Dim student1 As New Student With {.FirstName = "Svetlana",
                                         .LastName = "Omelchenko",
                                         .Scores = New Integer() {98, 92, 81, 60}}
            Dim student2 As New Student With {.FirstName = "Claire",
                                        .LastName = "O'Donnell",
                                        .Scores = New Integer() {75, 84, 91, 39}}
            Dim student3 As New Student With {.FirstName = "Cesar",
                                        .LastName = "Garcia",
                                        .Scores = New Integer() {97, 89, 85, 82}}
            Dim student4 As New Student With {.FirstName = "Sven",
                                        .LastName = "Mortensen",
                                        .Scores = New Integer() {88, 94, 65, 91}}

            Dim arrList As New ArrayList()
            arrList.Add(student1)
            arrList.Add(student2)
            arrList.Add(student3)
            arrList.Add(student4)

            ' Use an explicit type for non-generic collections
            Dim query as system.collections..Generic.IEnumerable(Of Student) = From student As Student In arrList
                        Where student.Scores(0) > 95
                        Select student

            For Each student As Student In query
                Console.WriteLine(student.LastName & ": " & student.Scores(0))
            Next
            ' Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.")
            Console.ReadKey()
        End Sub

    Try the above example using Console and Option Strict


    Friday, August 12, 2011 5:07 PM

Answers

  • you can cast but a little bit cumbersome as the generic list is the solution since version 2005 for this which extended the arraylist

    Use

    Dim students as List(0f student) instead as Dim arrList as new ArrayList()

    The studends List keeps than a strongly typed arraylist of students.

     


    Success
    Cor
    • Proposed as answer by DiegoCattaruzza Friday, August 12, 2011 6:15 PM
    • Marked as answer by Mike Feng Tuesday, August 23, 2011 3:23 PM
    Friday, August 12, 2011 5:34 PM
  • I would just replace the arrList with:

    Dim studentList = New List(Of Student) From {student1, student2, student3, student4}

     

    The trouble with ArrayList objects are that they are not type-safe.  Option strict is giving a compile time error for something that could easily turn into a run-time error.  I imagine the code will run, but if the arrList object contains anything other than a student type it will blow up...

     

    I also prefer the method syntax over the query syntax.....but that is personal preference:

     

    Dim result = studentList.Where(Function(x) x.Scores.Where(Function(y) y > 95).Any()).ToList()

    Friday, August 12, 2011 7:51 PM

All replies

  • you can cast but a little bit cumbersome as the generic list is the solution since version 2005 for this which extended the arraylist

    Use

    Dim students as List(0f student) instead as Dim arrList as new ArrayList()

    The studends List keeps than a strongly typed arraylist of students.

     


    Success
    Cor
    • Proposed as answer by DiegoCattaruzza Friday, August 12, 2011 6:15 PM
    • Marked as answer by Mike Feng Tuesday, August 23, 2011 3:23 PM
    Friday, August 12, 2011 5:34 PM
  • I would just replace the arrList with:

    Dim studentList = New List(Of Student) From {student1, student2, student3, student4}

     

    The trouble with ArrayList objects are that they are not type-safe.  Option strict is giving a compile time error for something that could easily turn into a run-time error.  I imagine the code will run, but if the arrList object contains anything other than a student type it will blow up...

     

    I also prefer the method syntax over the query syntax.....but that is personal preference:

     

    Dim result = studentList.Where(Function(x) x.Scores.Where(Function(y) y > 95).Any()).ToList()

    Friday, August 12, 2011 7:51 PM
  • you can cast but a little bit cumbersome as the generic list is the solution since version 2005 for this which extended the arraylist

    Use

    Dim students as List(0f student) instead as Dim arrList as new ArrayList()

    The studends List keeps than a strongly typed arraylist of students.

     


    Success
    Cor

    Hi Cor,

    Better to add a NEW in there.

    Dim students As New List(Of Student)

     

    to avoid an exception error.

    ;-)

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Or see
    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Friday, August 12, 2011 9:29 PM