Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Keeping Secrets : Page 2


advertisement
Build an Example
If you want to try this yourself, first create a class that contains a string and an integer that will contain the text and the number of times each has appeared. Override the GetHashCode and ToString methods so that your class looks like this (I've used fields rather than properties, and added the ToString override, to keep things simple for this demonstration):

Public Class TextData Public Value As String Public Count As Integer Public Sub New(ByVal Value As String) Me.Value = Value Me.Count = 1 End Sub Public Overrides Function GetHashCode() _ As Integer Return Value.GetHashCode() End Function Public Overrides Function ToString() As String Return Me.Value & ":" & Me.Count End Function End Class

Add a method like the following to your project. The ParseFile procedure allows you to specify a file name, and returns the completed HashTable object:

Private Function ParseFile( _ ByVal fileName As String) _ As Hashtable ' Given a file name, parse the words out into ' a hash table and return it. Dim data As TextData Dim table As New Hashtable Dim reader As New StreamReader(fileName) Dim text As String = reader.ReadToEnd() ' Do some very cheap parsing. ' This could really benefit ' from regular expressions! Dim words() As String = _ text.Split(" ,.()[]{}".ToCharArray) For Each word As String In words If table.Contains(word) Then ' If the value is already in the ' hash table, just increment its Count ' property. data = DirectCast(table(word), TextData) data.Count += 1 Else ' The word isn't yet in the hash table. ' Create the TextData instance, data = New TextData(word) table.Add(word, data) End If Next Return table End Function

ParseFile does its work by first declaring the variables it will need, and then loads the text from the specified text file:



Dim data As TextData Dim table As New Hashtable Dim reader As New StreamReader(fileName) Dim text As String = reader.ReadToEnd()

The code performs some very cheap (that is, neither elegant nor accurate) text parsing, using the Split method, and then loops through all the words in the array it creates:

Dim words() As String = _ text.Split(" ,.()[]{}".ToCharArray) For Each word As String In words ' Code removed here... Next

Within the loop, the code checks each word to see if it's already in the hash table, and if so, retrieves the corresponding TextValue object and increments its Count property:

If table.Contains(word) Then ' If the value is already in the ' hash table, just increment its Count ' property. data = DirectCast(table(word), TextData) data.Count += 1 Else ' Code removed here... End If

If the word wasn't already in the HashTable, the code creates a new TextData object and adds it:

If table.Contains(word) Then ' Code removed here... Else ' The word isn't yet in the hash table. ' Create the TextData instance, data = New TextData(word) table.Add(word, data) End If

The procedure completes by returning the filled-in HashTable.

To test the ParseFile function, you could write code that passes it a file name and then loops through all the DictionaryEntry values in the returned HashTable, like this:

Dim table As Hashtable = _ ParseFile("C:\sessions.txt") For Each de As DictionaryEntry In table Debug.WriteLine(de.Value.ToString()) Next

If you wanted to retrieve just a specific item from the HashTable structure, you can index into the data using the key value, like this:

If table.Contains("Windows") Then Debug.WriteLine( _ CType(table("Windows"), TextData).Count) End If

The Item method (the default method in Visual Basic and the indexer in C#) returns the value associated with the supplied key, as an Object type. It's up to you to cast it as the type you need, and then you can retrieve any property of the object.

As you can see, using hashing and the HashTable class provided by the .NET Framework makes it simple to store values that you might later need to retrieve. Although we haven't talked much about storing secrets, using hash searching is a valid use for hashing. Next time, we'll investigate how you can use hashing to allow you to hide data, as well. Stay tuned!



Ken Getz is a senior consultant with MCW Technologies and splits his time between programming, writing, and training. He specializes in tools and applications written in Visual Studio .NET and Visual Basic. Ken is the author of the highly rated .Finalize() column in CoDe Magazine. He is also the co-author of several best-selling books, including Access 2002 Developer's Handbooks with Paul Litwin and Mike Gunderloy, Visual Basic Language Developer's Handbook with Mike Gilbert, and VBA Developer's Handbook with Mike Gilbert (Sybex). He co-wrote several training courses for Application Developer's Training Company, including VB.NET, ASP.NET, Access 2000 and 97, Visual Basic 6, and Visual Basic 5 seminars. He has also recorded video training for AppDev covering VB.NET, ASP.NET, VB6, Access 2000, and Access 97. Ken is a frequent speaker at technical conferences and has spoken often at Microsoft's Tech-Ed conference. Ken's also a technical editor for Access-VB-SQL Advisor magazine and a columnist for Informant Publications' asp.netPRO magazine..
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date