Check if list already Contains() object with equal key

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 4
Reputation: HalloJoe is an unknown quantity at this point 
Solved Threads: 0
HalloJoe HalloJoe is offline Offline
Newbie Poster

Check if list already Contains() object with equal key

 
0
  #1
May 3rd, 2009
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

  1. Public Class StringList
  2. Inherits List(Of String)
  3.  
  4. Public Overloads Sub Add(ByVal s As String)
  5. If Not Me.Contains(s) Then
  6. MyBase.Add(s)
  7. End If
  8. End Sub
  9.  
  10. End Class

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

  1. Public Class CustomList
  2. Inherits List(Of CustomObject)
  3.  
  4. Public Overloads Sub Add(ByVal o As CustomObject)
  5. ' If Not Me.Contains[an object with the same Key as the one im trying to add]?
  6. MyBase.Add(o)
  7. ' End If
  8. End Sub
  9.  
  10. End Class
  11.  
  12. Public Class CustomObject
  13. Public Key As String
  14. Public Num As Integer
  15. Public Ok As Boolean
  16. End Class

Hope you can help
O_o
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: HalloJoe is an unknown quantity at this point 
Solved Threads: 0
HalloJoe HalloJoe is offline Offline
Newbie Poster

Re: Check if list already Contains() object with equal key

 
0
  #2
May 3rd, 2009
Found it out - pretty simple

I created a class that implements IEqualityComparer:

  1. Public Class CustomEqualityComparer
  2. Implements IEqualityComparer(Of CustomObject)
  3.  
  4. Public Function Equals1(ByVal x As CustomObject, ByVal y As CustomObject) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of CustomObject).Equals
  5. Return String.Equals(x.key.Trim, y.key.Trim)
  6. End Function
  7.  
  8. Public Function GetHashCode1(ByVal obj As RssItem) As Integer Implements System.Collections.Generic.IEqualityComparer(Of CustomObject).GetHashCode
  9. Return obj.ToString().ToLower().GetHashCode()
  10. End Function
  11.  
  12. End Class

Then i use the comparer like this:

  1. Public Class CustomList
  2. Inherit List(Of CustomObject)
  3.  
  4. Public Overloads Sub Add(ByVal o As CustomObject)
  5. If Not Me.Contains(o, New CustomObjectEqualityComparer()) Then _
  6. MyBase.Add(o)
  7. End Sub
  8.  
  9. End Class
O_o
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 2
Reputation: tennisace24 is an unknown quantity at this point 
Solved Threads: 1
tennisace24 tennisace24 is offline Offline
Newbie Poster

Re: Check if list already Contains() object with equal key

 
0
  #3
May 3rd, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC