0

What's the proper way to use Collection object & of different datatype?

I'm getting type or namespace Collection<bool> could not be found error. Also found that member variable _isFormData is both bool, int and string at once. :-/

Saw this example, under accepted answer, at Web API: how to access multipart form values when using MultipartMemoryStreamProvider?

private Collection<bool> _isFormData = new Collection<bool>();  //bool...

_isFormData.Add(String.IsNullOrEmpty(contentDisposition.FileName));  //string...

for (int index = 0; index < Contents.Count; index++)
{
   if (_isFormData[index]) //int...
   { }
}
Community
  • 1
  • 1
fletchsod
  • 3,560
  • 7
  • 39
  • 65
  • 1
    There is no `Collection` type in `System.Collections.Generic` namespace, what type of collection do you want? `List` perhaps? – Ben Robinson Sep 08 '15 at 15:03
  • 1
    It's never a `string` or `int`. `String.IsNullOrEmpty` returns `bool` and you use an `int` to index the collection to pull out a `bool` value in `_isFormData[index]`. And you probably are missing a `using System.Collections.ObjectModel`. – juharr Sep 08 '15 at 15:03
  • 1
    @BenRobinson [System.Collections.ObjectModel.Collection](https://msdn.microsoft.com/en-us/library/ms132397(v=vs.110).aspx)? – juharr Sep 08 '15 at 15:04
  • @juharr: Ah! It all make sense now. It was confusing at first and Object Browser in Visual Studio returned no hit when I used "Collection". It works now. – fletchsod Sep 08 '15 at 15:09

2 Answers2

2

You need to have a using System.Collections.ObjectModel; line for the type to referenced correctly in your class.

The type is only ever a collection of booleans:

String.IsNullOrEmpty(contentDisposition.FileName)

tests whether contentDisposition.FileName is null or "" and returns true if so; false otherwise.

isFormData[index]

returns the bool value in the collection at element index.

David Arno
  • 42,717
  • 16
  • 86
  • 131
  • 2
    Actually it's just `using System.Collections.ObjectModel`. – juharr Sep 08 '15 at 15:10
  • Ah! It all make sense now. It was confusing at first and Object Browser in Visual Studio returned no hit when I used "Collection" keyword search. It works now. Now stuck waiting 5 more minutes for StackOverflow to let me accept an answer. – fletchsod Sep 08 '15 at 15:10
0

Here's an example to get you started. Note the "using System.Collections.ObjectModel"

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Program
{
   public static void Main (String[] args)
   {
       IList<Boolean> testCollection = new Collection<Boolean>();
       testCollection.Add(false);
       testCollection.Add(true);
       FillCollection(testCollection);

       for (int index = 0; index < testCollection.Count; index++)
       {
           if (testCollection[index])
           {
               Console.WriteLine("testCollection[{0}] is true", index);
           }
           else
           {
               Console.WriteLine("testCollection[{0}] is false", index);   
           }
       }
   }

   public static void FillCollection (IList<Boolean> collection)
   {
       Random random = new Random();
       for (int i = 0; i < 500; i++)
       {
           Boolean item = Convert.ToBoolean(random.Next(0, 2));
           collection.Add(item);
       }
   }
}
Cheese Lover
  • 460
  • 1
  • 5
  • 14