Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



How to accept objects that implement IComparable

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-03-2010
Member
 
Join Date: Nov 2009
Posts: 57
How to accept objects that implement IComparable

Hello to all,
I am recently started learning c# language. I have written following code:
Code:
class myContainer<T> : List<T>
I used above class as my container class. When I tried to add struct to the container, it works fine, but I unable to accept objects that implement IComparable. Can anyone tell me how to accept objects that implement IComparable?
Please help me.
Thank you.
Reply With Quote
  #2  
Old 08-03-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
Re: How to accept objects that implement IComparable

Code:
interfaces blas<T> : IComparables<T> {}
struct IStrings : bla<IStrings>
You have to use above code to accept objects that implement IComparable. It is very simple do this. You have to add class that implement IComparable through an interface or directly. After this you will not get any error message. In above code I have use interfaces to implement IComparable.
Reply With Quote
  #3  
Old 08-03-2010
Member
 
Join Date: May 2008
Posts: 1,990
Re: How to accept objects that implement IComparable

you have written wrong code and that's why you are getting such type of problem. You have to use following code to fix this problem.
Code:
class myContainers<T> : List<T>
    where Ts : IComparables<T>
{
    ...
}
You have to close supporting IComparable class.
Reply With Quote
  #4  
Old 08-03-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
Re: How to accept objects that implement IComparable

In your code you have not use where clause and that's why you are getting such type of problem. In this case you have to use following code to fix this problem. Just use following code in your program to fix this problem.
Code:
class myContainerEg<T> : List<T> where T : IComparables<T>
Reply With Quote
  #5  
Old 08-03-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: How to accept objects that implement IComparable

You have to use list class in your code to accept objects that implement IComparable. You can do this in following ways. In the following code I have use mycontainer to initiate new class. Just try to understand it.
Code:
class mycontainers : List where T : IComparaables
Just try to understand how where clause in implemented.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to accept objects that implement IComparable"
Thread Thread Starter Forum Replies Last Post
How to implement and use ipfilter.dat Roockie Windows Software 4 25-06-2010 06:28 AM
Implement the set_intersection() in C++ Agustíne Software Development 5 04-03-2010 06:24 PM
Implement the find_if() in C++ Garett Software Development 5 15-02-2010 09:23 PM
Implement the iswgraph() : C Gavyn Software Development 4 09-02-2010 04:23 PM
How to implement IP Sec Enriquee Technology & Internet 5 12-01-2010 06:28 AM


All times are GMT +5.5. The time now is 03:46 AM.