|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Scripting RuntimeHistorically, auntie virus software has issued false positives against code
using the Scripting Runtime. Does .NET have a native replacement for the Scripting runtime? For example, is there the equivalent of a "CompareMethod" for the Collection object as for the Dictionary object? Check out the classes in the System.IO namespace.
As far as CompareMethod, it still exists but many classes implement the IComparable interface to provide custom comparison logic. Read the docs on that interface, it is fairly simple. I'm primarily interested in Dictionary.
I was surprised that I have not yet found in VB .NET the equivalent of CompareMethod to affect the Add method of a Collection object. Or did I miss it somehow? You can use the SortedList for this. The keys in a sorted list are
sorted either by the IComparable implementation in the keys themselves, or by using an IComparer that is passed in when the list is created. Perhaps this is what you need. "Chris Dunaway" <dunaw***@gmail.com> wrote in message I need to insert items using case sensitive, but I then sort casenews:1112722592.737516.215130@o13g2000cwo.googlegroups.com... > You can use the SortedList for this. The keys in a sorted list are > sorted either by the IComparable implementation in the keys themselves, > or by using an IComparer that is passed in when the list is created. > > Perhaps this is what you need. > insensitive. Using Option Strict On with hte following sample snippet. Dim dict As Scripting.Dictionary Dim i As Integer Dim vnt() As Object dict = New Scripting.Dictionary With dict ..CompareMode = Scripting.CompareMethod.BinaryCompare ..Add("a", "a") ..Add("A", "A") vnt = DirectCast(.Items, Object()) End With strOutput = strOutput + "Dictionary: Should list ""a"" and ""A""" + vbCrLf For i = 0 To UBound(vnt) strOutput = strOutput + i.ToString + vbTab + DirectCast(vnt, _ Object())(i).ToString() + vbCrLf Next i dict = Nothing In the real app, I then do a QuickSort. Did you create the items you are sorting? If so, just make your class
implement the IComparable interface. You can have a property that indicates whether or not sorting should be case insensitive then just provide the logic in the CompareTo method of the IComparable interface. If you don't have control of the items you are sorting, then you need to make a separate class called MyItemComparer (for example) that implements the IComparer interface and put the compare logic there. Then use an instance of that class when you create the SortedList. When creating a SortedList you would instantiate the class like this: Dim MyComparer As New MyItemComparer(True) 'True = sort case sensitive Dim MySortedList As New SortedList(MyComparer) Here is a example (sorting logic not provided, watch for word wrap): Public Class MyItemComparer Implements IComparer Private bSortCaseSensitive As Boolean Public Sub New(Optional ByVal casesensitive As Boolean = False) MyBase.New() bSortCaseSensitive = casesensitive End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare 'Return the comparison result of the two objects If bSortCaseSensitive Then 'Compare x and y in a case sensitive fashion and return result Else 'Compare x and y in a case insensitive fashion and return result End If End Function End Class Of course, you would adjust the code to compare your items correctly. The Compare function should return -1 if the first item is less than the second, 0 if they are equal, and 1 if the first item is greater than the second. Check the docs for the IComparer interface to make sure. My issue is not with sorting, that's the easy part.
The issue is adding items to the list. I was just seeking a built-in replacement for the .Add method for a scripting Dictionary. If I could modify the Add method for the Collection object, that might work. Is there a way to modify the compare behavior for the Add method of the collection object. About the only way would be to create your own collection class and
derive it from CollectionBase or DictionaryBase. But the IComparer object I was talking about is used by the SortedList when you add items. That's how it knows the order in which to add them. "Chris Dunaway" <dunaw***@gmail.com> wrote in message I knew that, but the sorting is not the issue.news:1112811335.804285.231570@z14g2000cwz.googlegroups.com... > About the only way would be to create your own collection class and > derive it from CollectionBase or DictionaryBase. > > But the IComparer object I was talking about is used by the SortedList > when you add items. That's how it knows the order in which to add > them. I t seems strange that there is no built-in way to modify th ebehaviot of the Add method for the Collection object. I guess MSFT figured that since VB 6 got it wrong, might as well perpetuate the issue. Having to write one's own class for this often needed functionality is unfortunate.
ARRAYLIST ADDING A CLASS
VB.NET VERY Slow sockets O.T.:Shameless Plug to get Free Software(for me & for you too) Why "Application has generated an exception" error and not a good error message? How to check for scroll bars in web browser object? Transpose datagrid row to corresponding label text Deployment - using Registry items Upgrade from 6.0 (Printer) Inherited User Control + Subclassed components + Dissapear after build!!!!!!!!!!!!!!! |
|||||||||||||||||||||||