List in Vb.Net .Net 5

MRUTYUNJAYA MAHAPATRA 1 Reputation point
2021-04-20T04:30:11.97+00:00

I am migrating one Vb.Net Project into .Net 5. Following source code is giving compilation error. List is not getting resolved. giving compilation error >

Public Sub AddItem(ByVal item As ConfigItem)
If item Is Nothing Then
Throw New ArgumentNullException("item")
End If

    Dim listViewItem As **List** = New ListViewItem(item.Item) <List is not getting resolved. giving compilation error >   

    listViewItem.SubItems().Add(item.Required)   
    listViewItem.SubItems().Add(item.Description)   
    listViewItem.SubItems().Add("")   
    listViewItem.SubItems().Add(item.ConfigurationTool)   
    listViewItem.SubItems().Add(item.Instructions)   
    listViewItem.Tag = item   
    ListView1.Items.Add(listViewItem)   
End Sub   

Can anybody please let me know How can I migrate ?

Best Regards
Mrutyunjaya

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2021-04-21T10:34:01.723+00:00

    This is what will work with a VB .NET project set to use .NET 5 without adding any references to the project.

    Full source

    Language extension

    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    
    Public Module ListViewExtensions
        <Extension>
        Public Function GetSelectedItem(listView1 As ListView) As ListViewItem
            Return (If(listView1.SelectedItems.Count > 0, listView1.SelectedItems(0), Nothing))
        End Function
    End Module
    

    Example adding a new item

    Private Async Sub OnTraverseEvent(information As DirectoryItem)
    
        Await Task.Delay(100)
    
        FoldersListView.InvokeIfRequired(Sub(listView)
                                             Dim item = New ListViewItem(information.ItemArray)
                                             item.Tag = information
                                             listView.Items.Add(item)
                                         End Sub)
    
        ProcessingLabel.InvokeIfRequired(Sub(label)
                                             label.Text = $"{FoldersListView.Items.Count}"
                                         End Sub)
    
    
    End Sub
    

    Get current selection, here from a context menu

    Private Sub OpenFolderContextMenuStrip_Opening(sender As Object, e As ComponentModel.CancelEventArgs) _
        Handles OpenFolderContextMenuStrip.Opening
    
        If FoldersListView.Items.Count = 0 Then
            e.Cancel = True
        Else
            Dim selected = FoldersListView.GetSelectedItem()
        End If
    
    End Sub
    

    Project file (note references)

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net5.0-windows</TargetFramework>
        <RootNamespace>RecurseFolders</RootNamespace>
        <StartupObject>Sub Main</StartupObject>
        <UseWindowsForms>true</UseWindowsForms>
        <MyType>WindowsForms</MyType>
        <OptionStrict>On</OptionStrict>
      </PropertyGroup>
    
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</WarningsAsErrors>
      </PropertyGroup>
    
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
        <WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</WarningsAsErrors>
      </PropertyGroup>
    
      <ItemGroup>
        <Import Include="System.Data" />
        <Import Include="System.Drawing" />
        <Import Include="System.Windows.Forms" />
      </ItemGroup>
    
      <ItemGroup>
        <ProjectReference Include="..\FileHelpers\FileHelpers.vbproj" />
        <ProjectReference Include="..\FolderBrowserDialog\FolderBrowserDialog.csproj" />
      </ItemGroup>
    
      <ItemGroup>
        <Compile Update="My Project\Application.Designer.vb">
          <DesignTime>True</DesignTime>
          <AutoGen>True</AutoGen>
          <DependentUpon>Application.myapp</DependentUpon>
        </Compile>
        <Compile Update="My Project\Resources.Designer.vb">
          <DesignTime>True</DesignTime>
          <AutoGen>True</AutoGen>
          <DependentUpon>Resources.resx</DependentUpon>
        </Compile>
      </ItemGroup>
    
      <ItemGroup>
        <EmbeddedResource Update="My Project\Resources.resx">
          <CustomToolNamespace>My.Resources</CustomToolNamespace>
          <Generator>VbMyResourcesResXFileCodeGenerator</Generator>
          <LastGenOutput>Resources.Designer.vb</LastGenOutput>
        </EmbeddedResource>
      </ItemGroup>
    
      <ItemGroup>
        <None Update="My Project\Application.myapp">
          <Generator>MyApplicationCodeGenerator</Generator>
          <LastGenOutput>Application.Designer.vb</LastGenOutput>
        </None>
      </ItemGroup>
    
    </Project>
    
    0 comments No comments