| | |
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:
Solved Threads: 0
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
How can i do the same Contains check on my CustomObject.Key ?
Hope you can help
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
VB.NET Syntax (Toggle Plain Text)
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 ?
VB.NET Syntax (Toggle Plain Text)
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
O_o
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 0
Found it out - pretty simple 
I created a class that implements IEqualityComparer:
Then i use the comparer like this:

I created a class that implements IEqualityComparer:
VB.NET Syntax (Toggle Plain Text)
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:
VB.NET Syntax (Toggle Plain Text)
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
O_o
•
•
Join Date: Apr 2009
Posts: 2
Reputation:
Solved Threads: 1
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
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
![]() |
Similar Threads
- Basic question about linked lists (C++)
- JSP database connectivity according to Model View Controller (MVC) Model 2 (JSP)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the VB.NET Forum
- Previous Thread: Send Fax via FAXCOMLib com and modem
- Next Thread: tab control (easy question)
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2005 2008 access account application array arrays basic beginner browser button buttons center checkbox client code convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel fade filter forms ftp generatetags gridview html images inline input insert intel internet lib listview mobile monitor net objects open panel passingparameters pdf picturebox port position print printing problem read remove save searchvb.net select serial settings shutdown soap sorting sqlserver survey table temperature textbox timer timespan transparency trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winforms wpf wrapingcode year





