473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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
Nov 21 '05 #1
7 1552
"JohnR" <Jo******@hotmail.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
I believe you need to use the System.Reflection namespace classes to do
this.
"JohnR" <Jo******@hotmail.com> wrote in message
news:ldJje.10929$mv5.6377@trndny07...
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

Nov 21 '05 #3
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
"JohnR" <Jo******@hotmail.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
"JohnR" <Jo******@hotmail.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
"JohnR" <Jo******@hotmail.com> schrieb:
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=%23iSHyCDEFHA.3596%40TK2MSFTNGP12.phx. gbl>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
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

Nov 21 '05 #7
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


"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SteveS | last post by:
What's the difference in using the private variable as opposed to the actual property in a class module? Which is better or more efficient? ( LoginObject.Save (_loginName) or LoginObject.Save...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
3
by: WStoreyII | last post by:
I have a Collection Class That I am Creating , In This Collection Class I have a default Property Item. My Problem is this I have about twenty different Criterias that can be used as a value...
3
by: yxq | last post by:
Hello, I use the com component "shockwave flash object" to play flash file, but how to get the actual size (width*height) of flash file but no control size? thank you!
9
by: Stefan De Schepper | last post by:
Should I use: Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value
5
by: IcingDeath via DotNetMonster.com | last post by:
I am building this SQL Server database app in which i can store files. In order to display files I want to have the app show the associated icon for the extension of the file that is in the...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
0
by: hwDevelop | last post by:
I'm trying to programatically set the Tag value of each Textbox and combobox in my form to databinding field value (like: CustomerFirstName ) so i can utilize it for filtering purpose. Does...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.