Home » c# » c# – This for loop loops through the same values more then once?-Exceptionshub

c# – This for loop loops through the same values more then once?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

Why does this for loop loop through the same Values more then once? I addedd the categories Class to the code so that you can see what i mean with that they are actuall lists. Theese lists withholds values that are scrambled and that should be unscrambled depending on if they have the same three first numbers

     for(int i = 0; i < categories2.parentCategoryId.Count; i++)
     {
        for (int j = 0; j < categories2.NewCategoryId.Count; j++)
        {
            if(categories2.parentCategoryId[i].Substring(0, 3) == categories2.NewCategoryId[j].Substring(0, 3)){

                Console.Write(categories2.parentCategoryId[i] + " ");
                Console.Write(categories2.parentCategoryName[i] + "´          ");
                Console.Write(categories2.NewCategoryId[j] + " ");
                Console.Write(categories2.NewCategoryName[j] + "\n"); 

            }
        }
     }




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrestaConverter
{
    public class Categories
    {
        public List<string> CategoryName = new List<string>();
        public List<string> CategoryId = new List<string>();

        public List<string> NewCategoryId = new List<string>();
        public List<string> NewCategoryName = new List<string>();

        public List<string> oldCategoryName = new List<string>();
        public List<string> oldCategoryId = new List<string>();

        public List<string> parentCategoryId = new List<string>();
        public List<string> parentCategoryName = new List<string>();

        public List<string> childCategoryID = new List<string>();
        public List<string> childCategoryName = new List<string>();

        public Categories()
        {


        } 
        public bool checkFirstNumbers(string categoryid,string correctNumber)
        {
            for(int i = 0; i < 2; i++){
                if(categoryid.Substring(i) == correctNumber.Substring(i))
                {
                    return true; 
                }
            }



            return false; 

        }

    }
}

Okey so this is the full explanation i have 3 categories in full wich is 3 objects that withholds 6 different lists Categories2 is a list of a bunch of subcategories that should exist beneath categories1. So theese categories have two different identification ways either by id or name so we have NewCategoryId and NewCategoryName theese lists does not have two of the same values in them. But then we have the parencategoryName and parentCategoryId lists and theese withholds the parentcategoryNames in the ammount of times that they need to show up to be able to place the subcategories beneath em. and now we need to compare NewCategoryID with parentCategoryId to see where to place NewCategoryName in our exceldocument. that is the full story

How to&Answers:

To explain a bit, as mentioned in my comment, if both categories2.parentCategoryId.Count and categories2.NewCategoryId.Count are the same, your sample code could look like this:

 for(int i = 0; i < categories2.parentCategoryId.Count; i++)
 {
        if(categories2.parentCategoryId[i].Substring(0, 3) == categories2.NewCategoryId[i].Substring(0, 3)){

            Console.Write(categories2.parentCategoryId[i] + " ");
            Console.Write(categories2.parentCategoryName[i] + "´          ");
            Console.Write(categories2.NewCategoryId[i] + " ");
            Console.Write(categories2.NewCategoryName[i] + "\n"); 
    }
 }

Your code uses nested loops, which work like this:

for(int i = 0; i < 10; i++)
{
    // this is run 10 times
    for(int j = 0; j < 10; j++)
    {
       // this runs 10 times for each iteration of i, so all in all 100 times
    }
}

If I understand your needed functionality correctly and you use List<T> You can use something like:

 for(int i = 0; i < categories2.parentCategoryId.Count; i++)
 {
     var match = categories2.NewCategoryId.FirstOrDefault(stringToCheck => stringToCheck.Substring(0, 3) == categories2.parentCategoryId[i].Substring(0, 3)));
    if(match != null)
    {
            Console.Write(categories2.parentCategoryId[i] + " ");
            Console.Write(categories2.parentCategoryName[i] + "´          ");
            Console.Write(match  + " ");
            Console.Write(match  + "\n"); 
    }
 }

What should happen here is that the loop runs through the first list and in each iteration checks, if there exist an item with the same substring in the second list (FirstOrDefault either returns the first item that fits the result, or null if it doesn’t find any).
If a correct item is found, the same code as in OP is run.

Hope I didn’t make any mistakes, doesn’t have a code editor in front of me 😉