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 = ...
精彩评论