[dba-VB] Iterating class properties

jwcolby jwcolby at colbyconsulting.com
Tue Nov 13 11:14:25 CST 2007


Charlotte, Drew,

What I am trying to accomplish is to be able to dynamically, at run time,
get a list of all properties of a class.  This allows me to compare values
between any two (or more) class instances of any class.  Suppose (for
example) I wanted to merge 4 records in a table into a single table.  I
don't know what the field names are but I want to load the table records
into classes and then go comparing one record against the next.

Suppose I want to know the data type of a property of a class at run time?
I have a class I want to persist to a table.  The table does not exist yet.
I need to find out what data the table holds, build a table with fields to
hold that data.  I need to discover what the properties of the class are
(what data is read / writeable) and what types of data do they hold?  I can
then build SQL statements to build a table to match the class, right then,
on the fly.  Pass in a class, pop out a table in SQL Server.

I have found the answer it seems.  I am not sure I understand it yet though.

John W. Colby
Colby Consulting
www.ColbyConsulting.com 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Drew Wutka
Sent: Tuesday, November 13, 2007 11:55 AM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] Iterating class properties

What are you trying to accomplish?  I don't use .Net very often, still do
most of my stuff in VB 6.  But I wanted a way to dynamically capture my
class properties.

For example, let's say I had Widget, and it had Height and Width properties.
I wanted to be able to do this:

Dim wd as Widget
Set wd=new Widget
Wd.Properties("Height")=10

Or

Dim wd as Widget
Dim I as long
Set wd=new Widget
For i=1 to wd.Properties.Count
	Wd.Properties(i)=15
Next i

I don't know if .Net has this capability, VB 6 didn't natively, but I was
able to 'spoof' the ability.  I created an Add-in that let's me create a
Properties property for a class.  When you run the Add-in, it goes through
all of the properties in your class, and creates statements to simulate the
abilities above.  It's adding a lot of extra code to do this, but in some
situations I have run into, it's worth it...plus, all the properties are
left alone, so I can use them like I normally would.

Drew

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Tuesday, November 13, 2007 6:43 AM
To: dba-vb at databaseadvisors.com
Subject: [dba-VB] Iterating class properties

Does anyone know a way to iterate the properties of a class in VB.Net or
C#.Net (or in .Net in general)?  There is built-in functionality to store
classes out to XML files and this stuff does exactly that, apparently using
only the Get/Let statements, i.e. if there is not both a get and let
statement for a property then the process fails to convert that property to
XML.  

So if MS does it, is that capability publicly available?  Is there a
property iterator buried in the innards of the class object?

John W. Colby
Colby Consulting
www.ColbyConsulting.com 

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com

The information contained in this transmission is intended only for the
person or entity to which it is addressed and may contain II-VI Proprietary
and/or II-VI BusinessSensitve material. If you are not the intended
recipient, please contact the sender immediately and destroy the material in
its entirety, whether electronic or hard copy. You are notified that any
review, retransmission, copying, disclosure, dissemination, or other use of,
or taking of any action in reliance upon this information by persons or
entities other than the intended recipient is prohibited.


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list