Results 1 to 11 of 11

Thread: Using Distinct

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Using Distinct

    say

    dim ab as string()

    ab=ab.distinct ()

    works.

    Now turn into option explicit. Well, got error. Got to use ctype. Things become

    vb.net Code:
    1. ab= CType(ab.Distinct, String())

    Still got error

    System.InvalidCastException was unhandled
    Message=Unable to cast object of type '<DistinctIterator>d__81`1[System.String]' to type 'System.String[]'.

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Using Distinct

    Hi,

    I think you should add this:

    vb Code:
    1. Imports System.String
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Distinct

    You mean Option Strict, not Option Explicit.

    You're assigning the result of Distinct to a String() variable. With Oprion Strict On, that would require that Distinct returned a String(). Does it? Have you checked the documentation? As discussed in another of your threads, the Distinct method acts on objects that implement IEnumerable(Of T), so there's no reason that it should return an array. As also discussed, it's part of LINQ and LINQ is all about IEnumerable(Of T). Logic would suggest that LINQ would provide a way to turn an IEnumerable(Of T) into an array. You already know that Distinct is a member of the Enumerable class. Have you checked what other members that class has?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Using Distinct

    I still cannot rate jmcilhinney again. It bothers me. Why can't people that have helped me so much can't get better rating much faster.

    The documentation says that distinct returns System.Collections.Generic.IEnumerable(Of String)

    That make sense. After all there are many classes that implement System.Collections.Generic.IEnumerable(Of String)

    Error 2 Option Strict On disallows implicit conversions from 'System.Collections.Generic.IEnumerable(Of String)' to '1-dimensional array of String'. \\work\c\business\fromwork\currentprojects\program\library\vb.net\listManipulator.vb 1375 21 dreamhost

    What about trying the code your self.
    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.     Public Function fileToStringCol(ByRef file As String, Optional ByRef unique As Boolean = False) As System.Collections.Specialized.StringCollection
    4.         fileToStringCol = New System.Collections.Specialized.StringCollection
    5.         Dim strar() As String
    6.  
    7.  
    8.  
    9.  
    10.         strar = System.IO.File.ReadAllLines(file)
    11.         If unique Then
    12.             'strar = strar.Distinct - compile error
    13.             strar = CType(strar.Distinct, String()) ' run time error
    14.  
    15.             strar = CType(System.Linq.Enumerable.Distinct(Of String)(strar), String())
    16.         End If
    17.  
    18.         fileToStringCol.AddRange(strar)
    19.         'If unique Then
    20.         '    'fileToStringCol = makeUnique(fileToStringCol)
    21.         '    fileToStringCol = System.Linq.Enumerable.Distinct(Of String)(fileToStringCol)
    22.         'End If
    23.     End Function

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Using Distinct

    try this:

    vb Code:
    1. Dim strar() As String = IO.File.ReadAllLines(file).distinct.toarray

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Using Distinct

    what jmcilhinney was trying to tell you in his inimicable way in post #3 was that as you had already found the distinct method, you should search further + find the .toarray method of the IEnumerable(Of T)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Using Distinct

    Ahhh..... to array. Hmm... interesting.

    Why I do not have to do so again when option strict is off?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Using Distinct

    option strict off allows implicit conversion but isn't recommended

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Distinct

    With Option Strict On, the compiler does type-checking at compile time. As such, you have to cast and convert explicitly so that the compiler is satisfied that everything you use is the type it needs to be. With Option Strict Off, the compiler will let you get away with anything that could possibly work at run time. As long as it doesn't know for sure that a conversion won't work, it will not flag it as a compilation error. At run time, type-checking is done again and, if the types don't match, a conversion is performed automatically.

    Now, that may sound like Option Strict Off is better, but that's really not the case. It might be easier to write code that will compile because you don't have to think about types so much. It actually makes your code slower to execute though, because of the extra type-checking at run time. It also makes your code less robust, because an error that might otherwise have been picked up by the compiler slips through and only shows itself at run time. If you're not thinking about types when writing the code, which Option Strict On forces you to do, then you are more likely to use incompatible types and end up with an exception being thrown at run time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Registered User
    Join Date
    Mar 2015
    Posts
    9

    Re: Using Distinct

    Hi, sorry to bump this up but with Option Strict on, are there any new functions with the latest version?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Distinct

    Quote Originally Posted by ragortue View Post
    Hi, sorry to bump this up but with Option Strict on, are there any new functions with the latest version?
    Option Strict has worked exactly the same way in all versions of VB.NET from 2002 up to the soon-to-be-released 2015. If it's On then late-binding and implicit widening conversions are disallowed and if it's Off then they're allowed. That's all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width