Re: Unique an array of strings
- From: "Brian Tkatch" <Maxwell_Smart@xxxxxxxxxxxxxxx>
- Date: 2 Aug 2006 08:51:08 -0700
Mythran wrote:
"Brian Tkatch" <Maxwell_Smart@xxxxxxxxxxxxxxx> wrote in message
news:1154531218.502140.128060@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm looking for a simple way to unique an array of strings. I came up
with this. Does it make sense? Am i missing anything? (Testing seems to
show it to work.)
Public Function Unique(ByVal List() As String) As String()
' Returns the unique values of in array, in an array.
Dim Temp As New
System.Collections.Specialized.StringCollection()
For Each Current_String As String In List
If Not Temp.Contains(Current_String) Then
Temp.Add(Current_String)
Next
ReDim Unique(Temp.Count - 1)
Temp.CopyTo(Unique, 0)
End Function
B.
Public Function Unique(ByVal List As String()) As String()
Dim temp As StringCollection = New StringCollection()
For Each current As String in List
If Not temp.Contains(current)
temp.Add(current)
End If
Next
Return temp
End Function
You don't need to ReDim Unique ... you could just use the "Return" statement
to return the array.
Shortened it up a wittle...and afaik, this would be how you remove duplicate
array entries...
HTH,
Mythran
Thanx for the feedback.
Hmm.. aren't you returning a StringCollection, when the Function
declared an array of String? Is there a way to do that? I put it back
into an array to return the same type.
As for the Redim, i thought it would be nicer to explicitly set the
size. But mostly, VB didn't like me using the function name without
doing something with it first (the green line, and at run time i got an
error about not instantiating it yet.)
B.
.
- Follow-Ups:
- Re: Unique an array of strings
- From: Mythran
- Re: Unique an array of strings
- References:
- Unique an array of strings
- From: Brian Tkatch
- Re: Unique an array of strings
- From: Mythran
- Unique an array of strings
- Prev by Date: Re: Publish Version number
- Next by Date: Re: Directly Passing Variables
- Previous by thread: Re: Unique an array of strings
- Next by thread: Re: Unique an array of strings
- Index(es):
Relevant Pages
|