[RESOLVED] Overriding Hashtable's Item(KEY) Property
Hi,
I am using a Hashtable which stores unique names and objects associated with those names.
Now those objects have a property called "IsDynamic" and some of them have it set to false whie some of them have it set to true.
So when I retrieve any object using the key property, it goes to find me that object. How do I override that find method?
Code:
Dim pTest As Hashtable
pTest = New Hashtable
pTest.Add("Test1", "Blah1")
pTest.Add("Test2", "Blah2")
pTest.Add("Test3", "Blah3")
Dim sTest As String
sTest = pTest("Test2")
I want to override the method which does this part
so I can check certain things of the object first and if needed then call a method of that object before giving that object back to the user.
Hope this makes sense. If any questions then please ask. I am ok to create my object inheriting from Hashtable too.
Cheers :)
Re: Overriding Hashtable's Item(Get) Property
You shouldn't really be using a Hashtable at all in VB 2008. For standard work you should use a generic Dictionary. For custom work, there are various base classes in System.Collections and its child namespaces that provide various functionality you can inherit and extend.
Re: Overriding Hashtable's Item(Get) Property
Thanks Jim.
This is an old piece of code that I am trying to modify and was written in VS.NET 2003. For the moment I am stuck with it.
Re: Overriding Hashtable's Item(Get) Property
A more better and working code for more help.
Code:
Public Class FormMain
Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim pTest As Manager
pTest = New Manager
MessageBox.Show(DirectCast(pTest.Test("Test1"), MyObject).IsDynamic.ToString())
MessageBox.Show(DirectCast(pTest.Test("Test2"), MyObject).IsDynamic.ToString())
MessageBox.Show(DirectCast(pTest.Test("Test3"), MyObject).IsDynamic.ToString())
' What I want to do is anytime when pTest.Test("KEY-NAME") is called, it checks if the object's IsDynamic is set to True, if so, it calls the object's RefreshData method before giving the object back
End Sub
End Class
Public Class Manager
Private FTest As Hashtable
Public ReadOnly Property Test() As Hashtable
Get
Return FTest
End Get
End Property
Public Sub New()
FTest = New Hashtable
Dim pMyObject As MyObject
pMyObject = New MyObject(Me, "Test1", False)
pMyObject = New MyObject(Me, "Test2", True)
pMyObject = New MyObject(Me, "Test3", False)
End Sub
Friend Sub AddToList(ByVal AMyObject As MyObject)
FTest.Add(AMyObject.Name, AMyObject)
End Sub
End Class
Public Class MyObject
Private FName As String
Private FIsDynamic As Boolean
Public ReadOnly Property IsDynamic() As Boolean
Get
Return FIsDynamic
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return FName
End Get
End Property
Private Sub RefreshData()
' Do something here
End Sub
Public Sub New(ByVal AParent As Manager, ByVal AName As String, ByVal AIsDynamic As Boolean)
FName = AName
FIsDynamic = AIsDynamic
AParent.AddToList(Me)
End Sub
End Class
Re: Overriding Hashtable's Item(Get) Property
You can't override a member that isn't declared Overridable. You pretty much have two choices:
1. Create a wrapper class for your Hashtable with a property/method that gets an item, processes it and then returns it.
2. Inherit the Hashtable class and shadow the Item property.
Just note that, if you go for the second option, you MUST always use references of your own type. If you cast as type Hashtable, which you can do, then you'll be using the original Hashtable.Item property implementation again.
Re: Overriding Hashtable's Item(Get) Property
Thanks Jim. Could you give me a quick example of both solution so I can weigh them individually? I think I don't have a choice but to implement my own class inheriting from the Hashtable.
Re: Overriding Hashtable's Item(Get) Property
Jim,
Never mind. I have restructured the code to avoid this whole thing. Refractoring is the king here! Thanks for the help and a valuable suggestion.
Cheers,
WRACK