简体   繁体   中英

get distinct values from array ( as String not List or ArrayList) in visual basic

All of the values of labels are stored into an array when the button is clicked

Dim myArr(3) As String           
myArr(0) = 1.Text  
myArr(1) = 2.Text
myArr(2) = 3.Text  
myArr(3) = 4.Text 

I am trying to remove duplicates from my array of labels, I've watched tutorials about distinct but it shows List and ArrayList. I've also coded for removing duplicates from my array

Dim testDist As String = myArr.Distinct().ToString  
           For Each NOW As String In testDist  
                Dim labelShow As String = String.Join(",", NOW)  
                Label7.Text = labelShow  
            Next  

But it only shows ] as output. Can you please help me.

You were on the right track with String.Join but you should have just passed the result of Distinct directly:

Label7.Text = String.Join(",", myArr.Distinct())

For the record, Disinct returns an IEnumerable(Of String) , ie a list that can be enumerated. String.Join will accept basically any enumerable list of Strings or Objects , including arrays or generis Lists . It the items are not Strings , their ToString methods will be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM