|
-
Dec 3rd, 2010, 05:55 AM
#1
Thread Starter
Fanatic Member
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:
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[]'.
-
Dec 3rd, 2010, 06:02 AM
#2
Re: Using Distinct
Hi,
I think you should add this:
-
Dec 3rd, 2010, 06:44 AM
#3
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?
-
Dec 4th, 2010, 09:12 AM
#4
Thread Starter
Fanatic Member
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:
Option Strict On Option Explicit On Public Function fileToStringCol(ByRef file As String, Optional ByRef unique As Boolean = False) As System.Collections.Specialized.StringCollection fileToStringCol = New System.Collections.Specialized.StringCollection Dim strar() As String strar = System.IO.File.ReadAllLines(file) If unique Then 'strar = strar.Distinct - compile error strar = CType(strar.Distinct, String()) ' run time error strar = CType(System.Linq.Enumerable.Distinct(Of String)(strar), String()) End If fileToStringCol.AddRange(strar) 'If unique Then ' 'fileToStringCol = makeUnique(fileToStringCol) ' fileToStringCol = System.Linq.Enumerable.Distinct(Of String)(fileToStringCol) 'End If End Function
-
Dec 4th, 2010, 12:34 PM
#5
Re: Using Distinct
try this:
vb Code:
Dim strar() As String = IO.File.ReadAllLines(file).distinct.toarray
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 4th, 2010, 12:38 PM
#6
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 4th, 2010, 12:40 PM
#7
Thread Starter
Fanatic Member
Re: Using Distinct
Ahhh..... to array. Hmm... interesting.
Why I do not have to do so again when option strict is off?
-
Dec 4th, 2010, 12:45 PM
#8
Re: Using Distinct
option strict off allows implicit conversion but isn't recommended
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 4th, 2010, 09:23 PM
#9
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.
-
Jul 2nd, 2015, 07:41 PM
#10
Registered User
Re: Using Distinct
Hi, sorry to bump this up but with Option Strict on, are there any new functions with the latest version?
-
Jul 4th, 2015, 01:10 AM
#11
Re: Using Distinct
 Originally Posted by ragortue
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|