question

MRUTYUNJAYAMAHAPATRA-6389 avatar image
0 Votes"
MRUTYUNJAYAMAHAPATRA-6389 asked karenpayneoregon answered

List in Vb.Net .Net 5

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

dotnet-csharpdotnet-visual-basicdotnet-runtime
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MRUTYUNJAYAMAHAPATRA-6389 ,

Dim listViewItem As **List** = New ListViewItem(item.Item)

What does ' **List** ' mean? Do you mean the following code :

     Dim listViewItem As ListViewItem = New ListViewItem(item.Item)

Besides, could you provide some related code about 'ConfigItem'? It will help us make a test.
We are waiting for your update.


0 Votes 0 ·

'ConfigItem' is a class. following is code of 'ConfigItem' class

Public Class ConfigItem

 Private _item As String
 Private _required As String
 Private _description As String

 Public Sub New(ByVal element As XElement)
     If element Is Nothing Then
         Throw New ArgumentNullException("element")
     End If

     If element.Name <> "Table" Then
         Throw New InvalidDataException("The configuration file is invalid.")
     End If

     _item = element.Element("Item")

 End Sub

 Public ReadOnly Property Item() As String
     Get
         Return _item
     End Get
 End Property

  ------------------------
 -------------------------


End Class

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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>





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.