Avatar of Tim Word
Tim Word
Flag for United States of America asked on

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.Globalization

Public 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 Function
End Class

Open in new window


Why do I get these if it works? How can I fix them?
Visual Basic.NETLINQ Query

Avatar of undefined
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 'Constructors

End Class

Open in new window


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.
SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Tim Word

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Tim Word

ASKER
it_saige pointed out that count was incorrect, so discovered that code should use .Any instead of .Count.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck