How can you get the actual Property name into a string?

  • Thread starter Thread starter JohnR
  • Start date Start date
J

JohnR

I have a user defined class that contains about 20 private variables with
their associated properties and get-sets. I have named the properties with
very user readable names. I would like to generate a datagrid with 2
columns. One column would have the actual name of the property, and the
other would have the value of the property.

So, if I have a property called "User_Name" with a value of "Bill" I would
want it to show up in the grid like this:

Property Value
User_Name Bill

Now, getting the value is easy, but how can I get the actual name of the
property into a variable???

Thanks,
John
 
JohnR said:
So, if I have a property called "User_Name" with a value of "Bill" I
would want it to show up in the grid like this:

Property Value
User_Name Bill

\\\
Imports System.Reflection
..
..
..
Public ReadOnly Property PropertyName() As String
Get
Return MethodBase.GetCurrentMethod().Name
End Get
End Property
///
 
I believe you need to use the System.Reflection namespace classes to do
this.
 
Thanks Herfried.. methodbase.getcurrentmethod().name seems like it's
close, but how would it actually be coded to retrieve the names of the
properties in the class.. For example, in the following class how could I
retrieve the property name "Name" and "Address"? My confusion is where do I
put the methodbase.getcurrentmethod().name code?

Public Class MyNewClass
Private _Name As String
Private _Address As String
Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Property Address() As String
Get
Return _Address
End Get
Set(ByVal Value As String)
_Address = Value
End Set
End Property
End Class

John
 
Oops, one more thing I forgot... Once I figure out how to get the actual
name of the property in the custom class, is there any way to
programatically iterate through all the properties in an instance of the
class? Something like this pseudocode:

for each i as property in MyClass.PropertyCollection
<do something with the property>
loop

Keep in mind that the whole reason I'm trying to do this is to create a
datagrid that contains the "property name" and "property value" for each of
the properties in the class instance. If I code it the way I would want to,
I could pass it any class instance and it would generate the datagrid (ie: I
don't know ahead of time what the properties or how many properties there
will be in the class instance).

Thanks,
John
 
JohnR said:
methodbase.getcurrentmethod().name seems like it's close, but how would it
actually be coded to retrieve the names of the properties in the class..
For example, in the following class how could I retrieve the property name
"Name" and "Address"? My confusion is where do I put the
methodbase.getcurrentmethod().name code?

'MethodBase' won't work in this context. The post referenced below will
give you an idea on how to iterate over a class' properties.

<URL:http://www.google.es/groups?selm=#[email protected]>
 
Have you looked at the PropertyGrid? It is designed for just that
purpose.

However, to get the property names for an object, you can use code
similar to this:

Imports System.Reflection

Dim MyType As System.Type = MyObject.GetType

Dim aProps() As PropertyInfo = MyType.GetProperties()

For Each pi As PropertyInfo in aProps
Debug.WriteLine(pi.Name)
Next
 
Thanks to all that replied and helped me solve this problem.

I did use the PropertyInfo in System.Reflections. Using P as the
propertyinfo variable, I was able to get both the name of the property as
well as it's value like this:

For Each p As PropertyInfo In
obj.GetType().GetProperties(BindingFlags.Public Or BindingFlags.Instance Or
BindingFlags.DeclaredOnly)
dr.Item(p.Name) = p.getvalue(obj, Nothing)
etc.

As you can see, I was able to retrieve both the name, p.Name, as well as the
value, p.getvalue(obj,nothing) for the property. The only thing that gave
me a little trouble was the 1st parameter of the getvalue method. It has to
be the "object" whose properties you are looking at. Also, since I am
scanning classes that are derived from other classes, I only wanted to "see"
the properties that I declared. Note the parameters on the GETPROPERTIES
method. The way they work is that you need to "or" all of the ones you
want. Specifying "bindingflags.declaredonly" by itself will not work...
Other than that, it all worked as expected. Thanks again to everybody.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top