Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 102981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:03:05+00:00 2026-05-11T01:03:05+00:00

After two years of C#, my VB.NET is a bit rusty. I have two

  • 0

After two years of C#, my VB.NET is a bit rusty. I have two Lists. Let’s call them originalList and targetList. Here is my C# code:

for(int i = 0; i<originalList.Count; i++) {     bool isMatch = false;     foreach (string t in targetList)     {         if(String.Compare(originalList[i], t, true) == 0)         {             isMatch = true;             break;         }     }     if(isMatch)     {         originalList.RemoveAt(i);         i--;     } } 

And my VB.NET code is this:

Dim i as Integer For i = 0 To originalList.Count - 1     Dim isMatch as boolean = false     For Each t As String In targetList         If String.compare(originalList(i), t, true) = 0 Then             isMatch = true             Exit For         End If     Next      If isMatch Then         originalList.RemoveAt(i)         i -= 1     End If Next 

But I got an index-out-of-range error with my VB.NET code. Where did I get it wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:03:06+00:00Added an answer on May 11, 2026 at 1:03 am

    Consider this – it’s a far more elegant way of achieving what you’re trying to do – and that is removing items from your original list that appear in your target list. Consider the following lists:

    Dim OriginalList As New List(Of String)(New String() {'a', 'b', 'c', 'd'}) Dim TargetList As New List(Of String)(New String() {'a', 'b', 'c'}) 

    And here’s how I’d remove all the items from the original that appear in the target…

    OriginalList.RemoveAll(Function(OriginalItem) TargetList.Contains(OriginalItem)) 

    Which in C# would be written:

    OriginalList.RemoveAll(OriginalItem => TargetList.Contains(OriginalItem)); 

    The less code you use to achieve a task, the less chance that coding bugs could be introduced.

    Side Note: This is very similar to an algorithm to test subsets. If you want to find out if set A is a subset of B, then you can iterate through B removing any corresponding items from A. Once you’ve finished iterating through B, if there are any items left in A, then it wasn’t a subset of B. If there are no items left, then it was a subset of B.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have once again fleshed out Ruby, after two years of not touching it,
Yesterday, after about two years we re-deployed our application. Code hasn't changed, but the
I have been doing Windows programming in .Net since last two years. Now I
After two years of Android development I'm still not 100% sure about what resources
I'm using ubuntu x64, after two days and searching all the net, still i've
I developed a program which after every two minutes call db to check new
I'm enjoying the Bootstrap tutorials here , but after the first two, I realize
After developing ASP.NET apps exclusively for several years I'm about to start developing Winforms
After almost two years of using DVCS, it seems that one inherent flaw is
I'm an experienced Java programmer that for the last two years have programmed for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.