473,396 Members | 2,029 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,396 software developers and data experts.

Iterating through class properties.

Hi all,

Just a quick question. I have a class that exposes a number of fields (which
are themselves custom types) through public properties.

At run time, I have an object whom I'd like to check, is of same type as one
of these fields.

Is there a method or technique for iterating through a class's public
properties, such as maybe using something from the reflection namespace?

This would probably be similar to iterating through the
System.Drawing.SystemColors class's properties.
Regards,
Nick
Nov 21 '05 #1
2 2151
Nick,

"Nick" <so*****@somewhere.com> schrieb:
Just a quick question. I have a class that exposes a number of fields
(which are themselves custom types) through public properties.

At run time, I have an object whom I'd like to check, is of same type as
one of these fields.

Is there a method or technique for iterating through a class's public
properties, such as maybe using something from the reflection namespace?


\\\
Public Class Foo
Private m_Property1 As String
Private m_Property2 As String
Private m_Property3 As Integer

Public Property Property1() As String
Get
Return m_Property1
End Get
Set(ByVal Value As String)
m_Property1 = Value
End Set
End Property

Public Property Property2() As String
Get
Return m_Property2
End Get
Set(ByVal Value As String)
m_Property2 = Value
End Set
End Property

Public Property Property3() As Integer
Get
Return m_Property3
End Get
Set(ByVal Value As Integer)
m_Property3 = Value
End Set
End Property
End Class

Private Sub SetAllStringPropertiesToFoo(ByVal Obj As Object)
For Each p As PropertyInfo In Obj.GetType().GetProperties( _
BindingFlags.Instance Or BindingFlags.Public _
)
If p.PropertyType Is GetType(String) Then
p.SetValue(Obj, "Foo", Nothing)
End If
Next p
End Sub
..
..
..
Dim foo As New Foo
With foo
.Property1 = "X"
.Property2 = "Y"
.Property3 = 22
SetAllStringPropertiesToFoo(foo)
MsgBox(.Property1)
MsgBox(.Property2)
MsgBox(.Property3.ToString())
End With
///

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

Nov 21 '05 #2
Excellent. Thanks :)
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Nick,

"Nick" <so*****@somewhere.com> schrieb:
Just a quick question. I have a class that exposes a number of fields
(which are themselves custom types) through public properties.

At run time, I have an object whom I'd like to check, is of same type as
one of these fields.

Is there a method or technique for iterating through a class's public
properties, such as maybe using something from the reflection namespace?


\\\
Public Class Foo
Private m_Property1 As String
Private m_Property2 As String
Private m_Property3 As Integer

Public Property Property1() As String
Get
Return m_Property1
End Get
Set(ByVal Value As String)
m_Property1 = Value
End Set
End Property

Public Property Property2() As String
Get
Return m_Property2
End Get
Set(ByVal Value As String)
m_Property2 = Value
End Set
End Property

Public Property Property3() As Integer
Get
Return m_Property3
End Get
Set(ByVal Value As Integer)
m_Property3 = Value
End Set
End Property
End Class

Private Sub SetAllStringPropertiesToFoo(ByVal Obj As Object)
For Each p As PropertyInfo In Obj.GetType().GetProperties( _
BindingFlags.Instance Or BindingFlags.Public _
)
If p.PropertyType Is GetType(String) Then
p.SetValue(Obj, "Foo", Nothing)
End If
Next p
End Sub
.
.
.
Dim foo As New Foo
With foo
.Property1 = "X"
.Property2 = "Y"
.Property3 = 22
SetAllStringPropertiesToFoo(foo)
MsgBox(.Property1)
MsgBox(.Property2)
MsgBox(.Property3.ToString())
End With
///

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

Nov 21 '05 #3

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

Similar topics

34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
5
by: RSH | last post by:
I have two HashTables (_CompanyDeductions,_CompanyAccruals) that contain several objects each. I am trying to loop through and print the properties of the objects in the Hashtables: I am trying...
7
by: =?Utf-8?B?RXZhbiBSZXlub2xkcw==?= | last post by:
I am a C++ programmer and have been learning C#. I have constructed a List<> and I want to iterate over it, altering each string in the list. In C++, I'd just create an iterator and walk the...
5
by: mikehulluk | last post by:
Ok, Imagine I have a class class C { }; ostream& operator<<(ostream& o, const C& c) { ...}
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
0
by: andi23coe | last post by:
Hello everybody! I have a problem with listing the properties of an unknown class via reflection so far. My class structure, automatically generated through linq to xsd: meeting --> VNR...
6
dmjpro
by: dmjpro | last post by:
As list can return the elements in which order they get stored. But in case of Set if I store the elements in an order the returning elements do not maintain the same order. Actually here is my...
9
by: TeenaRoz | last post by:
HI, I am new to perl and I am trying to write a perl script which does the following: Can someone help me? Firstly, There are a set of files named yyy.properties present in many folders in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.