Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-10-05
5
Medium Priority
?
38 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

722 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