VB.net Function works but shows errors - Why and how to fix
I have the following class. The function shows one error and one compiler warning.
Compiler warning:
Function doesn't return a value on all code paths.
Error:
Overload resolution failed because no accessible 'Where' can be called with these arguments:
Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic.KeyValuePair(Of Integer, Sheet), Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of Integer, Sheet))' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of System.Collections.Generic.KeyValuePair(Of Integer, Sheet), Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic.KeyValuePair(Of Integer, Sheet), Boolean)) As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of Integer, Sheet))' defined in 'System.Linq.Enumerable': 'Public ReadOnly Property Count As Integer' has no parameters and its return type cannot be indexed.
here is the class code:
Imports System.GlobalizationPublic Class Sheet Public Shared Sheets As New Dictionary(Of Integer, Sheet) Private Property Sheet As Integer Public Property Members As List(Of Member) Public Sub New(sheet As Integer) Members = New List(Of Member) Me.Sheet = sheet End Sub ''' <summary> ''' Generates the total weight of a sheet when the type of member ''' is passed to the function ''' </summary> ''' <param name="memberType">The type of member on a sheet, i.e. "Ladder", "Stair", etc</param> ''' <returns>Returns as a double the total weight of all members on a sheet for the MemberType</returns> ''' <remarks></remarks> Public Shared Function WeightByType(memberType As String) As Decimal Dim subset = Sheets.Where( Function(kvp As KeyValuePair(Of Integer, Sheet)) _ kvp.Value.Members.Count(Function(m) m.MemberType = memberType) > 0) subset.ToList().ForEach( Sub(kvp As KeyValuePair(Of Integer, Sheet)) _ WeightByType += (kvp.Value.Members.Sum( Function(m As Member) _ Convert.ToDecimal(m.TotalWeight, CultureInfo.InvariantCulture)))) End FunctionEnd Class
Why do I get these if it works? How can I fix them?
Visual Basic.NETLINQ Query
Last Comment
Tim Word
8/22/2022 - Mon
it_saige
What are the implementation details for Member and is Count an extension method since List.Count does not have an overload that accepts any parameters.
-saige-
Tim Word
ASKER
Member class:
Public Class Member#Region "Properties" Public Property Piecemark As String Public Property MemberType As String Public Property TotalWeight As String Public Property Size As String Private Property SheetKey As Integer Private Property Description As String Public Property StructType As String Private Property MemberSheetIndex As String#End Region 'Properties#Region "Constructors" 'Default class Constructor Public Sub New() Piecemark = String.Empty MemberType = String.Empty Size = String.Empty TotalWeight = String.Empty SheetKey = 0 StructType = String.Empty End Sub Public Sub New(ByVal piecemark As String, ByVal memberType As String, ByVal description As String, ByVal size As String, ByVal totalWeight As String, ByVal structType As String, ByVal memberSheetIndex As String, ByVal sheetId As Integer) Me.Piecemark = piecemark Me.MemberType = memberType Me.Description = description Me.Size = size Me.TotalWeight = totalWeight Me.StructType = structType Me.MemberSheetIndex = memberSheetIndex SheetKey = sheetId If Not Sheet.Sheets.ContainsKey(sheetId) Then Sheet.Sheets.Add(sheetId, New Sheet(sheetId)) End If Sheet.Sheets.Item(sheetId).Members.Add(Me) End Sub#End Region 'ConstructorsEnd Class
Sheets are filled from imported CSV in another class. The function is callled after the sheets are filled to get the total weight if the sheet contains a certain type of member, ie ladder, handrail, etc.
-saige-