 |
 |

07-20-2012, 02:07 PM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
|
|
Access Class Properties by a key
|
Someone asked me if you could access properties by a key as well as normally.
After playing around for a bit I came up with the following.
Not sure how useful it is...
Code:
Public Class clsSample
''' <summary>
'''<para>Access Properties by key</para>
''' <para>"Name", "Rank", "Rate"</para>
''' </summary>
Public Property Props As Hashtable = New Hashtable
Public Property Name As String = ""
Public Property Rank As Integer = 0
Public Property Rate As String = ""
Public Sub New(sName As String, nRank As Integer, sRate As String)
Name = sName
Rank = nRank
Rate = sRate
Props.Add("Name", Name)
Props.Add("Rank", Rank)
Props.Add("Rate", Rate)
End Sub
End Class
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|

08-02-2012, 03:13 AM
|
 |
Impetuous & volatile
* Expert *
|
|
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
|
|
It seemes to me that reflection is a better way to go around this.
Code:
Dim testObject As New clsSample()
Dim testType = testObject.GetType()
Dim wantedProperty = testType.GetProperty("Name")
Dim wantedValue = wantedProperty.GetValue(testType, null)
This way you reduce redundancy in your code. The problem with your implementation above is that you repeat yourself for each property, and furthermore with the specific code that is written, that the hash table's values isn't kept in sync with the properties' values.
|
__________________
Reading is the foundation for all knowledge - Unknown.
|

08-02-2012, 01:16 PM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
|
|
|
1) Actually the hash table values are in sync. I have tested it several times.
What we are adding to the hash table is the property objects themselves not the values.
2) Reflection can be slow. Especially when iterating over a number of objects in a collection. It has to be called for every one of them.
Using a hash table is very fast as it is a simple lookup.
In any case I cannot think of a situation where this tool would be useful, but thank you for the thoughts.
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|

08-02-2012, 01:51 PM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
|
|
|
Lookup is fast with a Hashtable, but you've lost type safety and have to perform casts. For value types, that means you're going to be boxing/unboxing, which can be very slow as well.
That said, there's not really a way to have a "property bag" without either reflection or boxing/unboxing. Personally I prefer reflection, because VB .NET is a type-safe language and designs that are not type-safe are "not .NET".
|
|

08-02-2012, 03:32 PM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
|
|
|
Thanks for the insight. That makes sense.
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|

08-03-2012, 02:01 AM
|
 |
Impetuous & volatile
* Expert *
|
|
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
|
|
|
Also, the properties do not stay in sync. I don't know how you tested it, but in your example, if you change the Name property after initializing an instance with the constructor then the Hashtabel will not be in sync. The values inside a class, however, will be kept in sync. But if you set the property to another instance, then it will not be synced either.
|
__________________
Reading is the foundation for all knowledge - Unknown.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|