locked
How to get property from code? RRS feed

  • Question

  • I have created my Class Customer, which contains 3 properties - Name, Number and Address.

    Now i want to make something like this:

    Dim Customer1 As Customer

    Customer1.Property(1) = "IBM"

    Customer1.Property(2) = 10

    Customer1.Property(3) = "Michigan 20, Ontario Street"

     

    That means I need to set property indirectly instead of directly (i.e. Customer1.Name = "IBM")

    Is this possible?

     


    ... Podlesnick ...
    Sunday, November 21, 2010 10:31 AM

Answers

  • You can do it without using Reflection

    Here an example that use the exact same syntax showed in your question

     

    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim Customer1 As New Customer
    
        Customer1.property(1) = "IBM"
        Customer1.property(2) = 10
        Customer1.property(3) = "Michigan 20, Ontario Street"
    
      End Sub
    End Class
    
    Public Class Customer
    
      Public WriteOnly Property [property](ByVal Index As Integer) As Object
        Set(ByVal value As Object)
          Select Case Index
            Case 1
              Me.Name = CType(value, String)
            Case 2
              Me.Number = CType(value, Integer)
            Case 3
              Me.Address = CType(value, String)
          End Select
        End Set
      End Property
    
    
      Private _Name As String
      Public Property Name() As String
        Get
          Return _Name
        End Get
        Set(ByVal value As String)
          _Name = value
        End Set
      End Property
    
      Private _Number As Integer
      Public Property Number() As Integer
        Get
          Return _Number
        End Get
        Set(ByVal value As Integer)
          _Number = value
        End Set
      End Property
    
      Private _Address As String
      Public Property Address() As String
        Get
          Return _Address
        End Get
        Set(ByVal value As String)
          _Address = value
        End Set
      End Property
    End Class
    
    • Proposed as answer by Omie Sunday, November 21, 2010 3:54 PM
    • Marked as answer by Kee Poppy Friday, November 26, 2010 10:47 AM
    Sunday, November 21, 2010 3:02 PM

All replies

  • I'm not sure but probably you can't set values of properties by index because properties are not stored in any container.
    Sunday, November 21, 2010 11:14 AM

  • It sounds like you really are trying to get into "Reflection".  This is the term you need for searching on how to do this in ..Net.  There is a class for setting a Property value using Reflection documented here: http://msdn.microsoft.com/en-us/library/xb5dd1f1.aspx
     
    Just be aware that using Reflection will always be slower than directly addressing properties.

    --
    Mike
    Sunday, November 21, 2010 12:20 PM
  • Hi Podlesnick,

    I believe the bit of reflection below will work for you.

    I have created a small class called Customer.

     

    Imports System.Reflection
    Public Class Customer
    
     Private _Name As String
     Private _Number As String
     Private _Address As String
    
     Public Property Name() As String
      Get
       Return _Name
      End Get
      Set(ByVal value As String)
       _Name = value
      End Set
     End Property
    
     Public Property Number() As String
      Get
       Return _Number
      End Get
      Set(ByVal value As String)
       _Number = value
      End Set
     End Property
    
     Public Property Address() As String
      Get
       Return _Address
      End Get
      Set(ByVal value As String)
       _Address = value
      End Set
     End Property
    
     Public Sub SetPropertyValue(ByVal Index As Int32, ByVal Value As String)
    
      Dim t As Type = Me.GetType
      Dim PropertyCount As Int32 = 0
    
      ' this will loop through each property (even Readonly Properties)
      For Each pi As PropertyInfo In t.GetProperties
    
       ' keep a track of the index of the current property
       PropertyCount += 1
    
       ' if we have a match then set the value, *IF* the value can be written to
       ' ie. is not ReadOnly
       If propertyCount = index Then
        If pi.CanWrite = True Then pi.SetValue(Me, Value, Nothing)
       End If
    
      Next
     End Sub
    
    End Class
    
    

    on the main form use the following to set the property.

    Customer1.SetPropertyValue(1, "Dogs")
    
    

    hope this answers your question

    Regards

     

    Dogs


    Regards Dogs
    Sunday, November 21, 2010 2:31 PM
  • You can do it without using Reflection

    Here an example that use the exact same syntax showed in your question

     

    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim Customer1 As New Customer
    
        Customer1.property(1) = "IBM"
        Customer1.property(2) = 10
        Customer1.property(3) = "Michigan 20, Ontario Street"
    
      End Sub
    End Class
    
    Public Class Customer
    
      Public WriteOnly Property [property](ByVal Index As Integer) As Object
        Set(ByVal value As Object)
          Select Case Index
            Case 1
              Me.Name = CType(value, String)
            Case 2
              Me.Number = CType(value, Integer)
            Case 3
              Me.Address = CType(value, String)
          End Select
        End Set
      End Property
    
    
      Private _Name As String
      Public Property Name() As String
        Get
          Return _Name
        End Get
        Set(ByVal value As String)
          _Name = value
        End Set
      End Property
    
      Private _Number As Integer
      Public Property Number() As Integer
        Get
          Return _Number
        End Get
        Set(ByVal value As Integer)
          _Number = value
        End Set
      End Property
    
      Private _Address As String
      Public Property Address() As String
        Get
          Return _Address
        End Get
        Set(ByVal value As String)
          _Address = value
        End Set
      End Property
    End Class
    
    • Proposed as answer by Omie Sunday, November 21, 2010 3:54 PM
    • Marked as answer by Kee Poppy Friday, November 26, 2010 10:47 AM
    Sunday, November 21, 2010 3:02 PM