Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 14th, 2007, 02:45 PM
shapper
Guest
 
Posts: n/a
Default Generic List. Remove duplicate

Hello,

I have an Enum and a Generic.List(Of Enum)

1 Public Enum Mode
2 Count
3 Day
4 Month
5 End Enum

How can I loop, in the generic list, from the last item that was added
to the first item that was added and remove the duplicated values.

For example, if I add the following items:

1 Dim gl As New Generic.List(Of Mode)
2 gl.Add(Mode.Count)
3 gl.Add(Mode.Month)
4 gl.Add(Mode.Day)
5 gl.Add(Mode.Count)
6 gl.Add(Mode.Day)

I want to remove the items added in 5 and 6 because they are repeated
and they were added last.

Thanks,

Miguel

  #2  
Old May 14th, 2007, 04:05 PM
OJ
Guest
 
Posts: n/a
Default Re: Generic List. Remove duplicate

On 14 May, 14:42, shapper <mdmo...@gmail.comwrote:
Quote:
Hello,
>
I have an Enum and a Generic.List(Of Enum)
>
1 Public Enum Mode
2 Count
3 Day
4 Month
5 End Enum
>
How can I loop, in the generic list, from the last item that was added
to the first item that was added and remove the duplicated values.
>
For example, if I add the following items:
>
1 Dim gl As New Generic.List(Of Mode)
2 gl.Add(Mode.Count)
3 gl.Add(Mode.Month)
4 gl.Add(Mode.Day)
5 gl.Add(Mode.Count)
6 gl.Add(Mode.Day)
>
I want to remove the items added in 5 and 6 because they are repeated
and they were added last.
>
Thanks,
>
Miguel
Miguel,
it might make more sense to restrict adding duplicates than to remove
them after adding....

http://msdn2.microsoft.com/en-us/library/ms132319.aspx

Look at the Example paragraph,.

hth,
OJ


  #3  
Old May 15th, 2007, 05:55 AM
=?Utf-8?B?YnVybG8=?=
Guest
 
Posts: n/a
Default Re: Generic List. Remove duplicate

I agree with OJ. However if there is a reason why you can not do this then a
way to remove duplicates from the list is sort the list with the sort
function and then call the RemoveAll function with a predicate that compares
the previous mode with the current mode shown below:


private static Mode temp;
private static bool RemoveDuplicate(Mode currentMode)
{
bool isDuplicate;
if (temp == currentMode)
{
isDuplicate = false;
}
else
{
isDuplicate = true;
}
temp = currentMode;
return isDuplicate;

}

list.sort();
list.RemoveAll(RemoveDuplicate);


"OJ" wrote:
Quote:
On 14 May, 14:42, shapper <mdmo...@gmail.comwrote:
Quote:
Hello,

I have an Enum and a Generic.List(Of Enum)

1 Public Enum Mode
2 Count
3 Day
4 Month
5 End Enum

How can I loop, in the generic list, from the last item that was added
to the first item that was added and remove the duplicated values.

For example, if I add the following items:

1 Dim gl As New Generic.List(Of Mode)
2 gl.Add(Mode.Count)
3 gl.Add(Mode.Month)
4 gl.Add(Mode.Day)
5 gl.Add(Mode.Count)
6 gl.Add(Mode.Day)

I want to remove the items added in 5 and 6 because they are repeated
and they were added last.

Thanks,

Miguel
>
Miguel,
it might make more sense to restrict adding duplicates than to remove
them after adding....
>
http://msdn2.microsoft.com/en-us/library/ms132319.aspx
>
Look at the Example paragraph,.
>
hth,
OJ
>
>
>
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.