0

I'm converting some C# code from another project to VB.Net but the following code is throwing an error

Dim obj As System.Collections.ArrayList()
obj = HTMLWorker.ParseToList(New StreamReader("../sample.htm",encoding.Default),
styles)

The error is 'Value of type Systems.Collections.Generic.List() cannot be converted to 'Systems.Collections.Arraylist()'

The original C# code is

System.Collections.ArrayList obj;
obj = HTMLWorker.ParseToList(new StreamReader("../sample.htm",Encoding.Default),
styles);

What would be the correct VB code?

1
  • 1
    You don't need (= shouldn't use) the () after ArrayList. Parenthesis are also used to declare arrays in VB, and this could lead to confusion. Commented Mar 29, 2011 at 10:07

1 Answer 1

4

I think this has nothing to do with C#/VB. It appears to me that

  • for your C# code, you used an earlier version of your library which returned an ArrayList and
  • for your VB code, you used a newer version that returned a generic list.

The simplest solution is to use type inference:

// C#
var obj = HTMLWorker.ParseToList(new StreamReader("../sample.htm", Encoding.Default), styles);

'' VB 
Dim obj = HTMLWorker.ParseToList(New StreamReader("../sample.htm", Encoding.Default), styles)

Note that to use this you need to have "Option Infer" set to "On" in your project properties.

If you don't want to use type inference, you need to declare obj with the correct type. To determine the correct type, either look up the documentation of ParseToList or read the information provided by IntelliSense when you type HTMLWorker.ParseToList(. For example, if ParseToList returns a generic List of IElements, the correct syntax is:

Dim obj As Systems.Collections.Generic.List(Of IElement)
obj = ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.