Solved

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

Posted on 2016-10-05
5
18 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 33

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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Refresh Datagridview from another form 1 34
Get String split 5 46
Visual Studio .Net 2012 and Crystal Reports 9 43
DataGridView Events ? 3 37
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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.

943 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

4 Experts available now in Live!

Get 1:1 Help Now