PDA

View Full Version : .NET ArrayList - HashTable - Dictionary Base



gmaxey
03-05-2017, 08:03 AM
Mana from Japan, I hope you see this question as it is related to a thread that you participated in last week.

http://www.vbaexpress.com/forum/showthread.php?58755-Particular-cut-paste-and-replace-for-red-text

While I dabble in VBA with Word mostly and occasionally in Excel an Outlook, I do not consider myself a programmer. Far from it. In fact, normal programming languages such as C+, Java, VB, .Net is to me for the most part a riddle wrapped in a mystery inside a enigma.

I had tinkered with the problem for a few minutes in the morning before headed out to school and thought that I would try to solve it using a scripting dictionary. The code below shows how I would have done that. However, when I saw your use of the .NET ArrayList, I became intrigued and thought I would see where I could take it and started researching it a bit.

I thought that if it worked ArrayList worked so well in a VBA then perhaps a .NET DictionaryBase might work as well. No luck. For whatever reason, CreateObject("System.Collections.DictionaryBase") errors and while CreateObject("System.Collections.HashTable") doesn't error, the methods don't seem to work.

Do you (or anyone) know if the .NET DictionaryBase or HashTable can work in VBA?

Thanks.



Sub ScrathcMacro()
'A basic Word macro code by Greg Maxey.
Dim oRng As Range, oRgSegment As Range
Dim oDictionary As Object
Dim lngIndex As Long
Dim strText As String
If Not Len(ActiveDocument.Range.Paragraphs.Last.Range) = 1 Then
ActiveDocument.Range.Paragraphs.Last.Range.InsertAfter vbCr
End If
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseStart
Set oDictionary = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
Do
lngIndex = 0
Do
oRng.MoveEnd wdParagraph
Loop Until Len(oRng.Paragraphs.Last.Range) = 1
Set oRgSegment = oRng.Duplicate
With oRgSegment.Find
.Font.Color = wdColorRed
Do While .Execute
If Not oRgSegment.InRange(oRng) Then Exit Do
oDictionary.Add fcnIntergerToLetter(lngIndex), oRgSegment.Text
With oRgSegment
.Text = ".........."
.Font.Color = wdColorAutomatic
.Collapse wdCollapseEnd
End With
lngIndex = lngIndex + 1
Loop
End With
strText = vbNullString
For lngIndex = 0 To oDictionary.Count - 1
strText = strText & "%" & oDictionary.Keys()(lngIndex) & ". " & oDictionary.Item(oDictionary.Keys()(lngIndex))
Next
With oRng
.Paragraphs.Last.Range.Text = vbCr & strText & vbCr + vbCr
.Collapse wdCollapseEnd
.Move wdParagraph, 3
End With
oDictionary.RemoveAll
Loop Until oRng.Paragraphs.Last.Range.End = ActiveDocument.Range.End
Application.ScreenUpdating = True
lbl_Exit:
Exit Sub
End Sub

Function fcnIntergerToLetter(lngCount As Long)
If lngCount = 0 Then
fcnIntergerToLetter = "a"
Else
If lngCount < 26 Then
fcnIntergerToLetter = LCase(Chr(lngCount + 1 + 64))
Else
fcnIntergerToLetter = "Error"
End If
End If
End Function

mana
03-06-2017, 05:41 AM
Sub test()
Dim hash As Object

Set hash = CreateObject("System.Collections.HashTable")

hash.Add 10, "ten"
hash.Add 1, "one"
hash.Add 2, "two"

MsgBox hash.contains(1)
MsgBox hash.containsvalue("two")
MsgBox hash.Item(10)
MsgBox hash.Count

hash.Clear
MsgBox hash.Count

End Sub

gmaxey
03-06-2017, 07:46 AM
mana,

Thanks. I feel pretty stupid. I had gotten as far as oHT.Add 2, "two" yesterday. As I mentioned, I wasn't getting an error like I was with Set oDic = CreateObject("System.Collections.DictionaryBase"), but I was watching the oHT object in the locals window and seeing this. I just assumed that the object wasn't taking my .Add attempts.

What about the DictionaryBase, any idea why it doesn't work while the others seem to be content?

mana
03-11-2017, 11:20 PM
Sorry for the late reply.
Only Partial functions of .NET Framework seems to be available.


I can't find english site
https://msdn.microsoft.com/ja-jp/library/dd313957.aspx