loop through properties?

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 56
Reputation: Tekito is an unknown quantity at this point 
Solved Threads: 0
Tekito Tekito is offline Offline
Junior Poster in Training

loop through properties?

 
-1
  #1
Nov 26th, 2008
I'm not quite sure how to tackle this problem. I have a form that displays the properties of a class. I have events for all the properties so the form gets updated automatically when the properties change. However, upon initializing a class, these events do not get called, and so the form starts out blank. What is the best way to program so that when I have a new class, the form displays the class's properties immediately?

(One idea I had was to loop through the properties and call each event... is there a way to loop through class properties in .NET?)
Last edited by Tekito; Nov 26th, 2008 at 12:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: 4advanced is an unknown quantity at this point 
Solved Threads: 10
4advanced 4advanced is offline Offline
Junior Poster in Training

Re: loop through properties?

 
0
  #2
Nov 26th, 2008
Sure you can, see the implementation below. This example is a copy/paste code from my project in which I added a class "ValidationInstance" which holds a collection of items which should be checked (or préfilled before the handler IsValid will be raisen).
Dim iPropertyCount As Integer = 0
        For Each PropertyItem As PropertyInfo In yourClass.GetType().GetProperties()
            Select Case iPropertyCount
                Case 1 To 10
                    'add the properties of this class to then validation instance

                    Dim strToAdd As String = String.Empty
                    If Not PropertyItem.GetValue(yourClass, Nothing) = Nothing Then
                        strToAdd = PropertyItem.GetValue(yourClass, Nothing).ToString()
                    End If
                    yourClass.ValidationInstance.ValidateRequired(PropertyItem.Name, _
                                                        strToAdd.ToString)

            End Select

            iPropertyCount += 1
        Next
Last edited by 4advanced; Nov 26th, 2008 at 2:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 56
Reputation: Tekito is an unknown quantity at this point 
Solved Threads: 0
Tekito Tekito is offline Offline
Junior Poster in Training

Re: loop through properties?

 
0
  #3
Nov 26th, 2008
Okay, thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC