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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Dec 23, 2009, 8:28 AM
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();
}
srinivasPosted Dec 23, 2009, 8:53 AM
is there any possibility to give the field to check wheather the list contains the object of that field in the contains function itself
Nilanka DharmadasaPosted Dec 23, 2009, 8:37 AM
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.
srinivasPosted Dec 23, 2009, 8:32 AM
srinivasPosted Dec 23, 2009, 8:30 AM
srinivasPosted Dec 23, 2009, 8:26 AM
do you know about the extension method of contains
Hiren SoniPosted Dec 23, 2009, 8:19 AM
Hiren SoniPosted Dec 23, 2009, 8:09 AM
srinivasPosted Dec 23, 2009, 8:04 AM
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 SoniPosted Dec 23, 2009, 7:44 AM
srinivasPosted Dec 23, 2009, 7:26 AM
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 SoniPosted Dec 23, 2009, 6:53 AM