Results 1 to 4 of 4

Thread: [resolved] loop through all properties [/resolved]

  1. #1

    Thread Starter
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    [resolved] loop through all properties [/resolved]

    is there any way to loop through all the properties of an object?

    Example: if i had an object of this class

    VB Code:
    1. public class ObjectExample
    2.  
    3. private _a as integer
    4. private _b as string
    5.  
    6. public property a() as integer
    7.  get
    8.     return _a
    9.  end get
    10.  set(byval value as integer)
    11.    _a = value
    12.  end set
    13. end property
    14.  
    15. public property b() as string
    16.  get
    17.     return _b
    18.  end get
    19.  set(byval value as string)
    20.    _b = value
    21.  end set
    22. end property
    23.  
    24. end class

    now i want to be able to loop throught and check the value of property a and then of property b.

    is this even possible?
    Last edited by dogfish227; Jun 23rd, 2004 at 07:19 PM.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Something like this:

    Make sure you have a Imports System.Reflection at the top.
    VB Code:
    1. Dim PropInfo As PropertyInfo()
    2.         Dim pi As PropertyInfo
    3.         Dim tmpObject() As Object
    4.         PropInfo = Me.GetType.GetProperties()
    5.         For Each pi In PropInfo
    6.             If pi.DeclaringType.Name = "ObjectExample" Then
    7.                 MsgBox((pi.Name & " : " & pi.GetValue(Me, tmpObject)))
    8.             End If
    9.         Next

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Or
    VB Code:
    1. Imports System
    2. Module Module1
    3.  
    4.     Sub Main()
    5.         Dim props() As System.Reflection.PropertyInfo
    6.         Dim objProp As System.Reflection.PropertyInfo
    7.         Dim objPerson As Person = New Person
    8.  
    9.         props = objPerson.GetType.GetProperties
    10.  
    11.         For Each objProp In props
    12.             Console.WriteLine("Property Name: " & objProp.Name _
    13.                 & "; Type: " & objProp.PropertyType.Name)
    14.         Next
    15.     End Sub
    16. End Module
    17.  
    18. Public Class Person
    19.  
    20.     Private mName As String
    21.     Public Property Name() As String
    22.         Get
    23.             Return mName
    24.         End Get
    25.         Set(ByVal Value As String)
    26.             mName = Value
    27.         End Set
    28.     End Property
    29.  
    30.     Private mAge As Integer
    31.     Public Property Age() As Integer
    32.         Get
    33.             Return mAge
    34.         End Get
    35.         Set(ByVal Value As Integer)
    36.             mAge = Value
    37.         End Set
    38.     End Property
    39.  
    40. End Class
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4

    Thread Starter
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    thanks

    thanks that worked great

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width