简体   繁体   中英

Error when trying to convert C# code to VB.Net

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?

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 IElement s, the correct syntax is:

Dim obj As Systems.Collections.Generic.List(Of IElement)
obj = ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM