|
-
April 10th, 2008, 10:02 AM
#1
How to restrict data passed
I are coding a generic class that works as red black balanced binary tree. I wanna post it here when finished.
(Is a shame that .NET does not have it predefined, like the array class.)
That data structure will store objects types, to be sure that the user can store his own objects.
My class declare the user object this way:
Code:
Public Module Red_Black_Tree
Private Class Node
Public Data As Object 'Each Data to store in this tree
...
But I need to force the user to use only Object witch implements the Icomparable interface.
I have two questions:
1-¿Is there a way to declare this line
Code:
Public Data As Object
to restrict it only to objects with Icomparable interface (or another workaround).?
2-When the new() subroutine is called, to create a new data, I can Try utilize the Icomparable interface, and if it does not have it, then trow an error (surely the same exception on the Try...End Try block of code), but I would like to have a specific way to check that it implements the Icomparable interface.
Last edited by Marraco; April 10th, 2008 at 10:13 AM.
-
April 10th, 2008, 10:58 AM
#2
Re: How to restrict data passed
How about a ListOf() with type-safety?
-
April 10th, 2008, 10:59 AM
#3
Re: How to restrict data passed
Im not pretty sure what are you exactly doing, but I think you are not taking the right direction in this one.
However, here are my responses to your questions.
1) You can declare the object to be a IComparable
Code:
Dim obj_Data As IComparable
This obj_Data will only accept instances of objects that have IComparable
2) When you want to check if an object has the IComparable interface you do this boolean expression:
Code:
If TypeOf obj_Data Is IComparable Then
Of course you can erase the IF-THEN.
-
April 10th, 2008, 11:50 AM
#4
Re: How to restrict data passed
 Originally Posted by Havok
Im not pretty sure what are you exactly doing, but I think you are not taking the right direction in this one.
I want to store generic Object variables on a specific data structure. "Physically" I plan to store an array of those objects, but that must be transparent to the programmer, who just need to know that (since I are doing a balanced binary tree) each new data is inserted automatically sorted.
That structure of data is fast to insert new data, and to search data.
The user gonna add new data with a line like:
Code:
MyTree.Add(MyObject)
Thus the class gonna have a Sub like:
Code:
Public Sub Add(NewItem as Object)
'Code to add the NewItem to efficient data structure.
'NewItem needs to implements an Icomparable interface,
'Since I need to access his .CompareTo function
end sub 'Add
 Originally Posted by Havok
However, here are my responses to your questions.
1) You can declare the object to be a IComparable
Code:
Dim obj_Data As IComparable
It looks like the answer: "Public Sub Add(NewItem as IComparable)" I gonna try that 
 Originally Posted by Havok
...
2) When you want to check if an object has the IComparable interface you do this boolean expression:
Code:
If TypeOf obj_Data Is IComparable Then
....
It also looks like the answer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|