alreadyused Posted May 18, 2007 Posted May 18, 2007 I am looking for feedback on this code/idea. What I want to do is load tables and throw away tables as needed. Some tables I will want to keep loaded for a while. For example, at runtime I find that i need a table so I try to get it from an array. If it's there, get it, if not load it (this isn't shown below; I add 5 dummy tables for testing) When I'm done with it (whenever that may be) I remove it. I started with the code basically mimicking PlausiblyDamp's suggestion in this post: http://www.xtremedotnettalk.com/showthread.php?t=83236 It sorted fine, but when I went to do a search on the array, it crashed because I was passing the table name and not a "TableClass", and it was trying to cast a string to type TableClass. Again starting from one of PD's suggestions from here: http://www.xtremedotnettalk.com/showthread.php?t=82561 I tested the object passed in to make sure it has a toString function. Sooooo, here is a very basic test for what I'm wanting to do: Module modMain Sub Main() Dim oTest As New FindTest Dim name As String For i As Integer = 1 To 5 name = "a" & i.ToString oTest.addTable(name) Next Dim tbl As TableClass = oTest.findTable("a1") MessageBox.Show(tbl.getName) oTest.removeTable("a3") End Sub End Module Public Class FindTest Private tbl() As TableClass Public Sub addTable(ByVal name As String) Dim xd As New Xml.XmlDocument Dim o As New TableClass(name, xd) Dim i As Integer If Me.tbl Is Nothing Then i = 0 Else i = Me.tbl.GetUpperBound(0) + 1 End If ReDim Preserve Me.tbl(i) Me.tbl(i) = o Me.tbl.Sort(Me.tbl) End Sub Public Function findTable(ByVal name As String) As TableClass Dim i As Integer = Me.tbl.BinarySearch(Me.tbl, name) Return tbl(i) End Function Public Function removeTable(ByVal name As String) As TableClass Dim t1() As TableClass Dim count As Integer = -1 Dim i As Integer = Me.tbl.BinarySearch(Me.tbl, name) If i >= 0 Then For j As Integer = 0 To Me.tbl.Length - 1 If j <> i Then count += 1 ReDim Preserve t1(count) t1(count) = Me.tbl(j) End If Next End If Me.tbl = t1 End Function End Class Public Class TableClass Implements IComparable Private name As String Private table As Xml.XmlDocument Public Sub New(ByVal name As String, ByVal table As Xml.XmlDocument) Me.name = name Me.table = table End Sub Public Function getName() As String Return Me.name End Function Public Function getTable() As Xml.XmlDocument Return Me.table End Function Public Overrides Function toString() As String Return Me.name End Function Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Try Dim name As String = obj.ToString Catch ex As Exception Throw New InvalidOperationException("Objects must have toString function for comparison.") End Try If String.Compare(Me.name, name) < 0 Then Return -1 If String.Compare(Me.name, name) > 0 Then Return 1 Return 0 End Function End Class This code compiles and runs fine; does what I want it to do. What I'm curious about is: Does this make sense to be doing this? Is there an easier or more efficient way to remove an item from an array? Am I doing the Compare correctly? Does anyone see any other errors or things I should improve on? Thanks in advance! Quote
MrPaul Posted May 18, 2007 Posted May 18, 2007 Consider a Dictionary Personally I would suggest using a Dictionary or Hashtable to associate each TableClass with its name. Unless you have a need to sort the tables besides faster access (which the Dictionary provides) this would definitely be preferable. It would also take care of resizing the collection as tables are inserted or removed, and more efficiently than your code appears to do. On an unrelated note, all objects expose a ToString method so the try/catch block would be unnecessary. Good luck :) Quote Never trouble another for what you can do for yourself.
alreadyused Posted May 18, 2007 Author Posted May 18, 2007 Re: Consider a Dictionary awesome on the try/catch! figures, i looked over this and was thinking, ok, i'm not doing anything stupid... ooops I don't need the tables sorted other than for fetching or removing. I'll check out the dictionary, thanks! Quote
alreadyused Posted May 22, 2007 Author Posted May 22, 2007 Re: Consider a Dictionary wow.... so..... EASY!!!!!!!!! Thanks for the tip! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.