Solved

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

Posted on 2016-10-05
5
17 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
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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 32

Assisted Solution

by:it_saige
it_saige earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
it_saige pointed out that count was incorrect, so discovered that code should use .Any instead of .Count.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query. 10 43
Crystal Report for VS2013 17 38
Format column on datatable 7 15
Red error squiggly on vb.net 7 0
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now