By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,262 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,262 IT Pros & Developers. It's quick & easy.

Scripting Runtime

P: n/a
Historically, 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?
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.

Nov 21 '05 #2

P: n/a
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?
Nov 21 '05 #3

P: n/a
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.

Nov 21 '05 #4

P: n/a
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.


I need to insert items using case sensitive, but I then sort case
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.
Nov 21 '05 #5

P: n/a
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.

Nov 21 '05 #6

P: n/a
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.
Nov 21 '05 #7

P: n/a
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.

Nov 21 '05 #8

P: n/a
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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 knew that, but the sorting is not the issue.

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.
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.