Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,
I have two generic types (T value) (T node). Now how do I know both starting with same integer or string or char???

Thanks
Posted

Short answer, without knowing anything about the type, you can't do any form of sorting or advanced value checking, because it would make no sense. (What if someone made a YourTree<double>? Or a YourTree<Socket>? Then looking for the first few characters would be meaningless.)

The exceptions are things that work on Object, so you can:
- group them together by value equality using Object.Equals or EqualityComparer as Kim says
- use their hashcode, though this is generally not useful
- use their string representation (ToString()), which might be what you want

It sounds like you want to restrict your T to classes which have particular operations on them, namely, providing a name, and possibly be comparable. That means you need an interface, for example
public interface ITreeNode : IComparable<ITreeNode> {
 string Name { get; }
}


... and then you need to restrict the T on your main class to support that interface:
public class MyBinaryTree<T> where T: ITreeNode {
...
}


Now, you can't create a MyBinaryTree<int> (int is not an ITreeNode), so you need to create ITreeNode subclasses for the data types you want to be in the tree. You can have a default one that looks like
public class TreeNode<T> : ITreeNode {
 T inner;

 public TreeNode(T inner) { this.inner = inner; }

 public int CompareTo(T other){
  if(inner is IComparable<T>) return ((IComparable<T>)inner).CompareTo(IComparable<T>)other);
  else return StringComparer.CurrentCulture.Compare(inner, other);
 }

 public string Name { get { return inner.Name; } }
}


... which wraps any type and uses its comparer if it has one, otherwise the string representation. (I think the native numeric types support IComparable<T> so they should sort correctly.) Now you can do
var myTree = new MyBinaryTree<TreeNode<int>>();

.. and within MyBinaryTree, you can use the methods of ITreeNode (i.e. the name), and also treat the nodes as IComparable<T> (i.e. pass them to sorting routines).

Hopefully that gives you enough information that you can work out exactly how to apply it to your situation.

E: Naturally, this was made up on the spot, not tested, and probably won't even compile. Also, generic types and HTML don't really play well together.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 17-May-11 18:34pm    
Good response, my 5. See my comment to the OP's comment to the answer by Kim.
I think it cannot help OP.
--SA
You can use Object.Equal[^] to call the compare ?

Or
EqualityComparer<T>.Default.Equals(x, y);
 
Share this answer
 
v2
Comments
Jayadheer Reddy 17-May-11 7:40am    
It just says whether both are equal or not. My requirement is match relatively like wanna group generics node which starts with "JE" as node name..
My actual problem is in generic binary search tree I have to group all nodes starts with same letter...
Jayadheer Reddy 17-May-11 7:41am    
Thanks for your prompt reply...
Kim Togo 17-May-11 7:44am    
You then need to know what type the generics node is ?
Jayadheer Reddy 17-May-11 7:51am    
I have to build a class Like an assembly.. that should work with any datatype...
what is the point of using Generics if we know what type we are dealing with... forgive me for ignorance...
One company technical manager in Melbourne asked me to solve this task.. tomorrow is my interview with him.. Junior/Mid web programmer is my position.. how pathetic.. I am really scared


Kim Togo 17-May-11 8:23am    
Google is your friend in need. Search up on all that about generic binary search tree and C#.
I do not know about how to compare and sort out on any datatype. You have to know what has to bee compared or sorted at.
Compare or sort a custom made class, the builder of the class has to override some method to give the correct info to sort on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900