Tim Word
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 .KeyValueP air(Of Integer, Sheet), Integer, Boolean)) As System.Collections.Generic .IEnumerab le(Of System.Collections.Generic .KeyValueP air(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 .KeyValueP air(Of Integer, Sheet), Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic .KeyValueP air(Of Integer, Sheet), Boolean)) As System.Collections.Generic .IEnumerab le(Of System.Collections.Generic .KeyValueP air(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:
Why do I get these if it works? How can I fix them?
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
Extension method 'Public Function Where(predicate As System.Func(Of System.Collections.Generic
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
Why do I get these if it works? How can I fix them?
ASKER
Member 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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it_saige pointed out that count was incorrect, so discovered that code should use .Any instead of .Count.
-saige-