0

I have a dll that has a function which requires a couple variables of Type Microsoft.VisualBasic.Collection. But I tried passing them as An Arraylist and A List in C# to no avail.

  Severity  Code    Description Project File    Line    Suppression State
  Error CS1502  The best overloaded method match for 
 'Object.RptOptimizer.BuildReport(string, string, System.Guid, 
 Microsoft.VisualBasic.Collection, Microsoft.VisualBasic.Collection, string, 
 string, System.DateTime, System.DateTime, string, string, string, string, 
 string, int, bool, int, int, bool)' has some invalid arguments EPWeb4   
 C:\inetpub\wwwroot\EPWeb4\Forms\RptParamsOptimizer.aspx.cs 271 Active

 Severity   Code    Description Project File    Line    Suppression State
 Error  CS0012  The type 'Collection' is defined in an assembly that is not 
 referenced. You must add a reference to assembly 'Microsoft.VisualBasic, 
 Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.    
Arraylist
  • 307
  • 3
  • 13
  • 3
    Maybe show your code of what you tried and the method signature of the function you are calling? – Ron Beyer May 11 '18 at 20:45
  • perhaps you can also show us some code? it will help to understand more – Francesco B. May 11 '18 at 20:45
  • 1
    According to the [docs](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.collection(v=vs.110).aspx) it's in `Microsoft.VisualBasic.dll` so see [How to add reference to Microsoft.VisualBasic.dll?](https://stackoverflow.com/q/21212194/3744182). In fact this might be a duplicate. Or just declare it as `IList` in c#. Then just declare it as `Microsoft.VisualBasic.Collection` in c#. Or declare it as an `IList` istead since `Collection` implements this interface. – dbc May 11 '18 at 20:47
  • By the way, you should prefer to use typed, generic collections instead. See [Benefits of Generics](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/benefits-of-generics) and [When would you not use Generic Collections?](https://stackoverflow.com/a/700688). – dbc May 11 '18 at 20:51

1 Answers1

0

The Microsoft.VisualBasic.Collection implements ICollection and IList just like a number of standard BCL collections do.

static void Main(string[] args)
{
    Microsoft.VisualBasic.Collection c = new Microsoft.VisualBasic.Collection();

    c.Add(new { Id = 1, Name = "John"});
    c.Add(new { Id = 2, Name = "Mary" });

    DoSomething(c);

    Console.ReadLine();
}

public static void DoSomething(ICollection collection)
{
    foreach (dynamic v in collection)
    {
        Console.WriteLine($"{v.Id} - {v.Name}");
    }
}

From the error above, you're also missing an assembly reference to the Microsoft Visual Basic assembly from the references page in your C# Project.

Right click the Project > Add > Reference ... > Assemblies Tab > Scroll down to Microsoft.VisualBasic and tick to include it.

Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157