Avatar of SillySoumare
SillySoumare
 asked on

I am trying to create to Item Properties in a class on that takes a string and one that takes an integer.

-----Comments----
I am getting an error.  I don't know what to put after the object, such as Implements System.Collections.
-----Comments----


 Default Public Overridable Overloads Property Item(ByVal pnKey As Integer) As Object
        Get
            Return DirectCast(MyBase.List.Item(pnkey), Object)
        End Get
        Set(ByVal Value As Object)
            MyBase.List.Item(pnkey) = Value
        End Set
    End Property

    Default Public Overridable Overloads Property Item(ByVal psKey As String) As Object
        Get
            Return DirectCast(MyBase.List.Item(psKey), Object)
        End Get
        Set(ByVal Value As Object)
            MyBase.List.Item(psKey) = Value
        End Set
    End Property
Visual Basic.NET

Avatar of undefined
Last Comment
SillySoumare

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jrscherer

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
tolgaong

What is the error?
Did you inherit this class from collectionbase?
If so, mybase.list.item accepts only integer.

In the overload that accepts string. you should write your logic.
E.G.

Default Public Overridable Overloads Property Item(ByVal psKey As String) As Object
        Get
            Return mybase.list.item(FindIndexByString(psKet))
        End Get
        Set(ByVal Value As Object)
            MyBase.List.Item(psKey) = Value
        End Set
    End Property

private function FindIndexByString(psKey as string) as integer
   'Write your logic here to find the index.
end function


By the way, you dont have to cast to the mybase.list.item to object as it is already an object.
SillySoumare

ASKER
Option Strict On disallows implicit conversions from 'String' to 'Integer'.

I inherited from CollectionBase

------------------------------------

Option Strict On

Public Class CCustomers
    Inherits CollectionBase

    Default Public Overridable Overloads Property Item(ByVal pnKey As Integer) As Object
        Get
            Return DirectCast(MyBase.List.Item(pnKey), Object)
        End Get
        Set(ByVal Value As Object)
            MyBase.List.Item(pnKey) = Value
        End Set
    End Property

    Default Public Overridable Overloads Property Item(ByVal psKey As String) As Object
        Get
            Return DirectCast(MyBase.List.Item(psKey), Object)
        End Get
        Set(ByVal Value As Object)
            MyBase.List.Item(psKey) = Value
        End Set
    End Property

    Public ReadOnly Property ItemByID(ByVal pnKey As Integer) As CCustomer
        Get
            Return CType(MyBase.List.Item(MyBase.List.IndexOf(Find(pnKey))), CCustomer)
        End Get
    End Property

    Public Sub Add(ByVal lobjCustomer As CCustomer)
        MyBase.List.Add(lobjCustomer)
    End Sub

    Public Sub Remove(ByVal pnIndex As Integer)
        Try
            If Exist(pnIndex) Then
                List.RemoveAt(pnIndex)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Shadows Sub RemoveAt(ByVal pnIndex As Integer)
        Try
            Remove(Item(pnIndex))
        Catch

        End Try
    End Sub

    Public Function Find(ByVal pnCustomerID As Integer) As CCustomer
        Dim lobjCustomer As CCustomer
        lobjCustomer = Nothing

        For Each lobjCustomer In MyBase.List
            If lobjCustomer.CustomerID = pnCustomerID Then
                Exit For
            End If
            lobjCustomer = Nothing
        Next

        Return lobjCustomer
    End Function

    Public Function Exist(ByVal psKeys As String) As Boolean
        Dim laKeys() As String
        Dim lnIndex As Integer
        Try
            Exist = False

            For lnIndex = laKeys.GetLowerBound(0) To laKeys.GetUpperBound(0)
                If laKeys(lnIndex).CompareTo(psKeys) = 0 Then
                    Exist = True
                    Exit For
                End If
            Next
        Catch ex As Exception

        End Try
    End Function

    Public Function GetCustomer(ByVal pnCustomerID As Integer) As Integer
        Dim lobjData As CDataTier
        Dim lnReturn As Integer

        Try
            lnReturn = ParseCustomer(lobjData.ReadCustomer(pnCustomerID))
        Catch ex As DataException
            lnReturn = -3
        Catch ex As Exception
            lnReturn = -4
        Catch ex As Exception
            Throw
        End Try

        Return lnReturn
    End Function

    Public Function ParseCustomer(ByVal pdsResult As DataSet) As Integer
        Dim lnReturn As Integer
        Dim lobjCustomer As CCustomer
        Dim lnCounter As Integer
        Try
            If pdsResult.Tables.Count <= 0 Then
                lnReturn = -1       'No tables were returned
            Else
                With pdsResult.Tables(0)
                    If .Rows.Count <= 0 Then
                        lnReturn = -2       'No rows were returned
                    Else
                        For lnCounter = 0 To .Rows.Count - 1
                            With .Rows(lnCounter)
                                lobjCustomer = New CCustomer(CType(.Item("CustomerID"), Integer), .Item("Prefix").ToString & "", .Item("FirstName").ToString & "", .Item("MI").ToString & "", .Item("LastName").ToString & "", .Item("Suffix").ToString & "", .Item("StreetAddress").ToString & "", .Item("City").ToString & "", .Item("State").ToString & "", .Item("ZipCode").ToString & "", .Item("ZipExtension").ToString & "", .Item("PrimaryPhone").ToString & "", .Item("SecondaryPhone").ToString & "", .Item("Email").ToString & "", .Item("SocialSecurity").ToString & "", .Item("DriverLicense").ToString & "", CType(.Item("BirthDate"), Date), .Item("BusinessName").ToString & "", .Item("MailAttention").ToString & "", .Item("Fax").ToString & "", CType(.Item("PrimaryExt"), Integer), CType(.Item("SecondaryExt"), Integer), CType(.Item("TaxID"), Integer))
                            End With
                            Add(lobjCustomer)
                            lobjCustomer = Nothing
                        Next
                    End If
                End With
            End If
        Catch ex As Exception
            Throw
        End Try

        Return lnReturn
    End Function
End Class
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck