Optimistic Peach Optimistic Peach - 1 year ago 45
C# Question

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

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download