Hi all!

I need to check if an object with equal property value(CustomObject.Key) exist in my custom list

In a List(Of String) i would do it like this

Public Class StringList
        Inherits List(Of String)

        Public Overloads Sub Add(ByVal s As String)
            If Not Me.Contains(s) Then
                MyBase.Add(s)
            End If
        End Sub

    End Class

How can i do the same Contains check on my CustomObject.Key ?

Public Class CustomList
        Inherits List(Of CustomObject)

        Public Overloads Sub Add(ByVal o As CustomObject)
            ' If  Not Me.Contains[an object with the same Key as the one im trying to add]?
            MyBase.Add(o)
            ' End If
        End Sub

    End Class

    Public Class CustomObject
        Public Key As String
        Public Num As Integer
        Public Ok As Boolean
    End Class

Hope you can help :)

Found it out - pretty simple :D

I created a class that implements IEqualityComparer:

Public Class CustomEqualityComparer
    Implements IEqualityComparer(Of CustomObject)

    Public Function Equals1(ByVal x As CustomObject, ByVal y As CustomObject) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of CustomObject).Equals
         Return String.Equals(x.key.Trim, y.key.Trim)
    End Function

    Public Function GetHashCode1(ByVal obj As RssItem) As Integer Implements System.Collections.Generic.IEqualityComparer(Of CustomObject).GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function

End Class

Then i use the comparer like this:

Public Class CustomList
    Inherit List(Of CustomObject)

        Public Overloads Sub Add(ByVal o As CustomObject)
                If Not Me.Contains(o, New CustomObjectEqualityComparer()) Then _
                        MyBase.Add(o)
        End Sub

End Class

I have a search in my program for names form a database and i wnat it to show all the information form name row in a datagrid but its only showing the name and not the information like address so how do i make show all information from that name row! From my query? Here is the code:

Public Class Form1

Private Sub DisplaySearchResults(ByVal objectname As String, ByVal FieldName As String, ByVal Searchdata As String)

Dim SQL_String As String

dbConnection = New OleDb.OleDbConnection(ConnectString)
dbAdapter = New OleDb.OleDbDataAdapter
dtSearch = New DataTable()

SQL_String = "select * from " & objectname & " where " & FieldName & "='" & Searchdata & "'" 'missing equals sign
dbAdapter = New OleDb.OleDbDataAdapter(SQL_String, ConnectString)
dbConnection.Open()
dbAdapter.Fill(dtSearch)
grdSearch.DataSource = dtSearch
dbConnection.Close()

End Sub

Private Sub btnWards_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWards.Click
grdHospital.DataSource = DisplayData("tblWards")

End Sub

Private Sub btnPatients_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatients.Click
grdHospital.DataSource = DisplayData("tblPatients")
End Sub

Private Sub btnPatientsWards_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatientsWards.Click
grdHospital.DataSource = DisplayData("qryPatientsByWard")
End Sub

Private Sub btnWardNameSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWardNameSearch.Click

Dim objectname As String
Dim Fieldname As String
Dim Searchdata As String

'objectname = "tblWards" 'missing s at end of tablename
objectname = "qryPatientsByWard" 'to get the patient name you need to use this query not the wards table
Fieldname = "WardName"
Searchdata = cboWardName.Text
DisplaySearchResults(objectname, Fieldname, Searchdata)
End Sub

Private Sub btnPatientSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatientSearch.Click
Dim ObjectName As String
Dim FieldName As String
Dim SearchData As String

ObjectName = "tblPatients"
FieldName = "PatientName"
SearchData = txtSearch.Text
DisplaySearchResults(ObjectName, FieldName, SearchData)

End Sub

Private Sub btnPatientAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatientAdd.Click
Dim SQL_Insert As String
SQL_Insert = "INSERT INTO tblPatients( PatientName, WardID )" & "VALUES ( '" & txtPatientName.Text & "', " & (cboWardName.SelectedIndex + 1) & ") "
ExecuteSQL(SQL_Insert)
grdHospital.DataSource = DisplayData("tblPatients")
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim intPatientID As Integer

If (IsNumeric(txtDelPatient.Text)) Then
intPatientID = CInt(txtDelPatient.Text)

Dim intUpdated As Integer

intUpdated = ExecuteSQL("DELETE FROM tblPatients WHERE PatientID = " & intPatientID)

If intUpdated > 0 Then
grdHospital.DataSource = DisplayData("tblPatients")
MsgBox("Patient Succesfully Deleted", MsgBoxStyle.Information, "Deleted")
Else
MsgBox("Patient not found in Table", MsgBoxStyle.Critical, "Error")
End If
Else
MsgBox("Invalid Patient ID", MsgBoxStyle.Critical, "Error")
End If
End Sub
End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.