"Cannot convert from bool to system.collections.generic.list<bool>" In what seems to be good code

Optimistic Peach

I am working on a compression algorithm for fun, but while working on a part of the code where I load some data to a class, I get an error, here is the code that gives the error:

    /// <summary>
    /// Loads an amount of bits from the filestream
    /// </summary>
    /// <param name="fs">A file from which to read the bits</param>
    /// <param name="amount">The amount of bits to read from</param>
    /// <param name="firstByte">The first byte from which to read some bits</param>
    /// <param name="firstByteOffset">The offset fot the first byte</param>
    /// <returns>The next byte and its offset in a tuple</returns>
    public Tuple<byte, int> loadBits(ref FileStream fs, int amount, byte firstByte, int firstByteOffset)
    {
        ///The bits to be added to the internal array
        List<bool> toBeAdded = new List<bool>();
        ///Add the first byte's data
        toBeAdded.AddRange(splitList(new List<bool>(convertByte8(firstByte)), firstByteOffset)[1]);
        ///Add the regular data in the center
        for(int i = 0; i!= (int)Math.Floor((double)amount - 1) / 8; i++)
        {
            toBeAdded.AddRange(convertByte8((byte)fs.ReadByte()));
        }
        ///Create a Tuple that contains the last byte and what it should be offsetted by the next time this method is run
        Tuple<byte, int> rtrnVal = new Tuple<byte, int>((byte)fs.ReadByte(), amount - ((int)Math.Floor((double)amount - 1) / 8) * 8);
        ///Add the bits from the last byte
        toBeAdded.AddRange(splitList(new List<bool>(splitList<bool>(/* error here */new List<bool>(convertByte8(rtrnVal.Item1))[0]/* To here */, rtrnVal.Item2))));
        ///Return
        return rtrnVal;
    }

The error is on the second last line of the method. It should just add the last byte's bits with the offset applied. I don't think it's necessary, but here are the two methods that are non-native (As in I made them):

    /// <summary>
    /// Splits a list into 2
    /// </summary>
    /// <typeparam name="T">The type of the list</typeparam>
    /// <param name="inList">The list to be split</param>
    /// <returns>2 List of half the size each</returns>
    public static List<List<T>> splitList<T>(List<T> inList, int splitIndex = 0)
    {
        splitIndex = Math.Abs(splitIndex);
        if (splitIndex == 0)
        {
            List<List<T>> newLists = new List<List<T>>(2);
            newLists[0] = new List<T>((int)Math.Ceiling((double)inList.Count / 2));
            newLists[1] = new List<T>((int)Math.Floor((double)inList.Count / 2));
            for (int i = 0; i != inList.Count; i++)
            {
                if (i > (int)Math.Ceiling((double)inList.Count / 2))
                {
                    newLists[1][i - (int)Math.Ceiling((double)inList.Count / 2)] = inList[i];
                    continue;
                }
                newLists[0][i] = inList[i];
            }
            return newLists;
        }
        else
        {
            List<List<T>> newLists = new List<List<T>>(2);
            newLists[0] = new List<T>(splitIndex);
            newLists[1] = new List<T>(inList.Count - splitIndex);
            for (int i = 0; i != inList.Count; i++)
            {
                if (i > splitIndex)
                {
                    newLists[1][i - splitIndex] = inList[i];
                    continue;
                }
                newLists[0][i] = inList[i];
            }
            return newLists;
        }
    }
    /// <summary>
    /// Converts a byte to 8 bits
    /// </summary>
    /// <param name="inByteA">The byte to be converted to bits</param>
    /// <returns>The 8 bit representation of inByteA</returns>
    static public bool[] convertByte8(byte inByteA)
    {
        bool[] result = new bool[8];
        string a = Convert.ToString(inByteA, 2);
        int temp = 0;
        foreach (char b in a)
        {
            result[temp] = b == '0' ? false : true;
            temp++;
        }
        return result;
    }

Thank you in advance for any errors there may be in my code that you may find.

PS. What does the ? after a variable mean?

S. Hooley

The issue is that the portion that is failing (new List<bool>(convertByte8(rtrnVal.Item1))[0]), refers to a single bool (the first bool in the array returned by convertByte8).

Part of why this is difficult to troubleshoot is because of your deeply-nested calls on that line that is failing. I'd recommend that, at least initially, you split those calls out into smaller steps, store the return values in local variables, and then pass those variables to the next call. This approach, while more verbose, will make it substantially easier to troubleshoot.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is good practice for installing code that will be JIT compiled?

From Dev

What is good about writing Java code in XML format as in Spring configuration?

From Dev

What is good way to validates Canadian Postal Code in Rails?

From Dev

What's the color code of Bad and Good Highlights on Excel VBA?

From Dev

What is a good way to code Java for multiple versions of Hadoop?

From Dev

What is a good tool to navigate large C# code base

From Dev

What is a good approach to share a code base between a webapp and batch applications?

From Dev

What is a good way to code Java for multiple versions of Hadoop?

From Dev

Awk seems to be confused what $1 is

From Dev

What `if ([ ])` turns out to be,it seems not falsy

From Dev

Code with regex seems to end for no reason

From Dev

This code seems to return all rows

From Dev

For loop seems to skip some code

From Dev

What is twitter:domain good for?

From Dev

What is a good alternative for gitbash?

From Dev

Solaris - What is it good for?

From Dev

What is object() good for?

From Dev

What good are python classes?

From Dev

What is chmod 6050 good for

From Dev

What is twitter:domain good for?

From Dev

What are containers in cloud good for?

From Dev

What is ICMP broadcast good for?

From Dev

Writing good Golang code

From Dev

What would be a good way to code a routine to show areas of HTML with AngularJS and Typescript?

From Dev

What's a good way to include code from a .rb file in my controller?

From Dev

What are good ways to work around the encoding limitation of SKAction code blocks during application state preservation?

From Dev

Calibre: what's a good format to convert to, in order to simplify cut and paste of code samples?

From Dev

What is a good character length for a code when we need 1 million unique variations?

From Dev

what is a good search strategy to look for source code of a particular function in chrome/firefox repository?

Related Related

  1. 1

    What is good practice for installing code that will be JIT compiled?

  2. 2

    What is good about writing Java code in XML format as in Spring configuration?

  3. 3

    What is good way to validates Canadian Postal Code in Rails?

  4. 4

    What's the color code of Bad and Good Highlights on Excel VBA?

  5. 5

    What is a good way to code Java for multiple versions of Hadoop?

  6. 6

    What is a good tool to navigate large C# code base

  7. 7

    What is a good approach to share a code base between a webapp and batch applications?

  8. 8

    What is a good way to code Java for multiple versions of Hadoop?

  9. 9

    Awk seems to be confused what $1 is

  10. 10

    What `if ([ ])` turns out to be,it seems not falsy

  11. 11

    Code with regex seems to end for no reason

  12. 12

    This code seems to return all rows

  13. 13

    For loop seems to skip some code

  14. 14

    What is twitter:domain good for?

  15. 15

    What is a good alternative for gitbash?

  16. 16

    Solaris - What is it good for?

  17. 17

    What is object() good for?

  18. 18

    What good are python classes?

  19. 19

    What is chmod 6050 good for

  20. 20

    What is twitter:domain good for?

  21. 21

    What are containers in cloud good for?

  22. 22

    What is ICMP broadcast good for?

  23. 23

    Writing good Golang code

  24. 24

    What would be a good way to code a routine to show areas of HTML with AngularJS and Typescript?

  25. 25

    What's a good way to include code from a .rb file in my controller?

  26. 26

    What are good ways to work around the encoding limitation of SKAction code blocks during application state preservation?

  27. 27

    Calibre: what's a good format to convert to, in order to simplify cut and paste of code samples?

  28. 28

    What is a good character length for a code when we need 1 million unique variations?

  29. 29

    what is a good search strategy to look for source code of a particular function in chrome/firefox repository?

HotTag

Archive