hi to all
can any one please explain how to use contain method for class objects
if i want to check wheather the employee name is there in employee object then how to write contains method
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 23, 2009, 3:28 AM EST
your Desired Code is Below
please check "Do you like this answer" checkbox if it helps you :)
Thank you :)
static void Main(string[] args)
{
LinkedList<LibraryClass> obj = new LinkedList<LibraryClass>();
obj.AddFirst(new LibraryClass(10, "srinivas", "fds", "dsffdsf"));
obj.AddFirst(new LibraryClass(20, "srini", "ffds", "ddsfsffdsf"));
obj.AddFirst(new LibraryClass(30, "sriniffsf", "ffdfsfds", "dfdsdsfsffdsf"));
Boolean flag = false;
foreach (LibraryClass l in obj)
{
if (l.BookName == "srinivas1")
{
Console.WriteLine("List Contain Book Name Srinivas");
flag = true;
break;
}
}
if (flag == false)
{
Console.WriteLine("Book Name Not Found");
}
Console.ReadKey();
}
Hiren SoniRank #694 · 3.5K reputationPosted Dec 23, 2009, 1:53 AM EST
srinivas137 reputationPosted Dec 23, 2009, 2:26 AM EST
that is iam storing my employe objects in ths linkedlist and now using Contains method how to know my linkedlist has an object which has the name="xyz"
i think you understood my problem
plz help me for this
thank you
Hiren SoniRank #694 · 3.5K reputationPosted Dec 23, 2009, 2:44 AM EST
srinivas137 reputationPosted Dec 23, 2009, 3:04 AM EST
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//this is my class
public class LibraryClass
{
#region Fields
int bookId;
string bookName;
string category;
string authorName;
#endregion
#region Properties
public int BookId
{
get { return bookId; }
set { bookId = value; }
}
public string BookName
{
get { return bookName; }
set { bookName = value; }
}
public string Category
{
get { return category; }
set { category = value; }
}
public string AuthorName
{
get { return authorName; }
set { authorName = value; }
}
#endregion
public LibraryClass()
{
}
public LibraryClass(int a,string b,string c,string d)
{
this.BookId = a;
this.BookName = b;
this.category = c;
this.AuthorName = d;
}
public override string ToString()
{
return string.Format("{0,-5}{1,-10}{2,-10}{3,-10}", BookId, BookName, Category, AuthorName);
}
}
class Program
{
static void Main(string[] args)
{
LinkedList
obj.AddFirst(new LibraryClass(10,"srinivas","fds","dsffdsf"));
obj.AddFirst(new LibraryClass(20, "srini", "ffds", "ddsfsffdsf"));
obj.AddFirst(new LibraryClass(30, "sriniffsf", "ffdfsfds", "dfdsdsfsffdsf"));
foreach (LibraryClass val in obj)
{
Console.WriteLine(val);
}
now i have to know wheather obj linkedlist has an Library class object with BookName="srinivas"
to know this how to write the Contains method
}
}
Hiren SoniRank #694 · 3.5K reputationPosted Dec 23, 2009, 3:09 AM EST
Hiren SoniRank #694 · 3.5K reputationPosted Dec 23, 2009, 3:19 AM EST
srinivas137 reputationPosted Dec 23, 2009, 3:26 AM EST
do you know about the extension method of contains
srinivas137 reputationPosted Dec 23, 2009, 3:30 AM EST
srinivas137 reputationPosted Dec 23, 2009, 3:32 AM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 23, 2009, 3:37 AM EST
I found the solution for you.
You should write your employee class as follows. Focus on the lines in red.
public class Employee : IEquatable
{
public string name;
public int age;
public override bool Equals(object obj)
{
return this.Equals(obj as Employee);
}
public bool Equals(Employee p)
{
// If parameter is null, return false.
if (Object.ReferenceEquals(p, null))
{
return false;
}
// Optimization for a common success case.
if (Object.ReferenceEquals(this, p))
{
return true;
}
// If run-time types are not exactly the same, return false.
if (this.GetType() != p.GetType())
return false;
return (name == p.name);//Here, if names are equals return true.
}
}
That means, I wrote a value equality for the employee. If the names are same of two employee objects it will return true for Equals method.
Now you can use Contains method.
Employee a = new Employee();
a.name = "abc";
a.age = 18;
Employee b = new Employee();
b.name = "sdf";
b.age = 18;
List
v.Add(a);
if (v.Contains(b)) //If b has the same name as a it will return true. Otherwise false.
{
}
If you have any problem let me know pls.
If you find this answer useful, please tick 'Do you like this answer' checkbox.
srinivas137 reputationPosted Dec 23, 2009, 3:53 AM EST
is there any possibility to give the field to check wheather the list contains the object of that field in the contains function itself