简体   繁体   English

尝试将C#代码转换为VB.Net时出错

[英]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 我正在将一些C#代码从另一个项目转换为VB.Net,但是以下代码引发错误

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()' 错误为“无法将类型为Systems.Collections.Generic.List()的值转换为'Systems.Collections.Arraylist()”

The original C# code is 原始的C#代码是

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

What would be the correct VB code? 什么是正确的VB代码?

I think this has nothing to do with C#/VB. 我认为这与C#/ VB没有关系。 It appears to me that 在我看来

  • for your C# code, you used an earlier version of your library which returned an ArrayList and 对于C#代码,您使用了较早版本的库,该库返回了ArrayList和
  • for your VB code, you used a newer version that returned a generic list. 对于您的VB代码,您使用了更新的版本,该版本返回了通用列表。

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. 如果您不想使用类型推断,则需要使用正确的类型声明obj 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: 要确定正确的类型,请在键入HTMLWorker.ParseToList(时查找ParseToList的文档或阅读IntelliSense提供的信息。例如,如果ParseToList返回IElement的通用List,则正确的语法为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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