Go Back  Xtreme Visual Basic Talk > General Discussion > Tech Discussions > Access Class Properties by a key


Reply
 
Thread Tools Display Modes
  #1  
Old 07-20-2012, 02:07 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default 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
Reply With Quote
  #2  
Old 08-02-2012, 03:13 AM
Qua's Avatar
Qua Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
Default

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.
Reply With Quote
  #3  
Old 08-02-2012, 01:16 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

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
Reply With Quote
  #4  
Old 08-02-2012, 01:51 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

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".
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 08-02-2012, 03:32 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Thanks for the insight. That makes sense.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #6  
Old 08-03-2012, 02:01 AM
Qua's Avatar
Qua Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
Default

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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->