Solved

VB.net Function works but shows errors - Why and how to fix

Posted on 2016-10-05
5
28 Views
Last Modified: 2016-10-11
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?
0
Comment
Question by:Tim Word
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 41830783
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-
0
 

Author Comment

by:Tim Word
ID: 41830804
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.
0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 500 total points
ID: 41830824
Okay now if you select Count in the line:
kvp.Value.Members.Count(Function(m) m.MemberType = memberType) > 0)

Open in new window


And press F12, what does the object browser (or code base) open to?

For example mine:Capture.JPGOpens to here:Capture.JPG
-saige-
0
 

Accepted Solution

by:
Tim Word earned 0 total points
ID: 41831671
Found my problem for the error of using .Count, should be .Any  like this:

        Dim subset =
                Sheets.Where(
                    Function(kvp As KeyValuePair(Of Integer, Sheet)) _
                                kvp.Value.Members.Any(Function(m) m.MemberType = MemberType))
        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))))

Open in new window

0
 

Author Closing Comment

by:Tim Word
ID: 41838096
it_saige pointed out that count was incorrect, so discovered that code should use .Any instead of .Count.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 44
Stop Git from being my repository 1 51
Best book for Internet security 4 55
Suppress if value zero or NULL in crystal report 2 44
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question