0

I used this website: https://www.codeconvert.ai/php-to-csharp-converter

to convert a piece of php code into:

using System;
using System.Collections.Generic;

namespace CodeTranslation
{
    class Program
    {
        static void Main(string[] args)
        {
            int length = 5;
            List<string> values = new List<string>();
            for (int i = 0; i < Math.Pow(2, length); i++)
            {
                values.Add(string.Format("{0:" + new string('0', length) + "}", Convert.ToString(i, 2)) + "<br>");
            }
            List<List<string>> pool = new List<List<string>>();
            for (int i = 0; i <= length; i++)
            {
                pool.Add(new List<string>());
            }
            List<string> final = new List<string>();
            foreach (string value in values)
            {
                int count = value.Split('1').Length - 1;
                pool[count].Add(value);
            }
            List<string> result = CalculateResult(pool);
            Console.WriteLine(string.Join("<br>", result));
        }

        static List<string> CalculateResult(List<List<string>> pool)
        {
            string start = "";
            int a = 0;
            int b = 1;
            List<string> result = new List<string>();
            while (result.Count != Math.Pow(2, 5))
            {
                List<string> poolA = pool[a];
                List<string> poolB = pool[b];
                if (poolA.Count == 0)
                {
                    a++;
                    b++;
                }
                string stringA;
                if (string.IsNullOrEmpty(start))
                {
                    stringA = poolA[0];
                    poolA.RemoveAt(0);
                }
                else
                {
                    stringA = result[result.Count - 1];
                    result.RemoveAt(result.Count - 1);
                    result.Insert(0, stringA);

But then at line 40. which is this one:

  List<string> poolB = pool[b];

it comes up with the message like in the title.

I have read the other stackof threads but I don't know how to exactly resolve this still.

  • also: why does it say that about poolB but not reg. poolA?
LTL
  • 339
  • 1
  • 9
  • 2
    did you use your debugger to see how many elements are in `poolA`? – MakePeaceGreatAgain Jul 05 '23 at 06:40
  • What should that code even do? – MakePeaceGreatAgain Jul 05 '23 at 06:42
  • It would be awesome if you had posted your complete code. I can't run what you've posted. – Enigmativity Jul 05 '23 at 06:52
  • 5, namely 11111. it generates a decision table of 5 parameters (int length = 5 if you change it to int length = 3 it generates a decision table of 3 parameters etc) – LTL Jul 05 '23 at 06:52
  • Don't do this: `result.Count != Math.Pow(2, 5)`. It coerces the `int into a `double` and then you just don't know if you're going to get a rounding issue. Just do `result.Count != 32`. – Enigmativity Jul 05 '23 at 06:53
  • @Enigmativity that doesn't do anything with the 'Index was out of range' – LTL Jul 05 '23 at 06:56
  • @LTL - You don't know that. The loop might continue as `32 != 32.0` might be true. In any case, it's bad to code it that way and I was just trying to help you with your coding style. – Enigmativity Jul 05 '23 at 07:12
  • @Enigmativity I know, and appreciate it. just saying it wasn't the solution. – LTL Jul 05 '23 at 07:15
  • We really need the full code sample. I suspect it's something to do with you removing strings as per `poolA.RemoveAt(0);` but for `poolB`. Essentially if you're doing something similar for poolB and there's nothing in it, when you come to try and remove something from it you will get this error. I would highly suggest debugging your code and stepping through it to see exactly what's going on. – sr28 Jul 05 '23 at 07:46
  • @LTL - But it could have been. – Enigmativity Jul 05 '23 at 09:15

1 Answers1

0

You must set a deep copy of the pool[a] in the poolA. please edit this line:

//List<string> poolA = pool[a];
List<string> poolA = pool[a].Select(x => x.ToString()).ToList();

Otherwise, the following line will cause the deleted item in the pool list, and you will get an out-of-range error.

 poolA.RemoveAt(0);
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
  • Tried everyting related to that, even change both like so: List poolA = pool[a].Select(x => x.ToString()).ToList(); List poolB = pool[b].Select(x => x.ToString()).ToList(); still produces the 'Index was out of range' – LTL Jul 05 '23 at 06:58
  • @LTL I placed `poolA` and `poolB` as deep copies. The program does not give errors. But this condition is always true `string.IsNullOrEmpty(start)` and while loop does not end. – Hossein Sabziani Jul 05 '23 at 07:04
  • I think I see what the problem is. The online tool doesn't seem to take all the lines as input. 20% seems to be missing... I gotta dive into this first. :S – LTL Jul 05 '23 at 07:34
  • When I use this site: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-7.0#code-try-3 and try the code then it says: program.cs(40,47): error CS1061: 'List' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?) but in VS it does not complain about that @Hossein Sabziani – LTL Jul 05 '23 at 11:25
  • @LTL - `Select` is part of LINQ. Add a using statement for `System.Linq`. – sr28 Jul 06 '23 at 10:25