Get class property value where name in variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rambalep

    #1

    Get class property value where name in variable

    I need to get a class property value, where property name is contained
    in a variable.

    example:
    class test
    dim p1 as string
    dim p2 as string
    end class
    ....
    sub x
    dim t as test
    dim myvar as string = "p1"
    ....
    'I need to read a property of object t and the name of property is in
    "myvar"
    ?

    How can I do it?
    Thanks

  • Carlos J. Quintero [VB MVP]

    #2
    Re: Get class property value where name in variable

    Class test
    Dim p1 As String
    Dim p2 As String

    Sub New(ByVal par1 As String, ByVal par2 As String)
    p1 = par1
    p2 = par2
    End Sub
    End Class

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim t As test
    Dim myvar As String = "p1"
    Dim objFieldInfo As System.Reflecti on.FieldInfo

    t = New test("A", "B")

    objFieldInfo = GetType(test).G etField(myvar,
    Reflection.Bind ingFlags.Instan ce Or Reflection.Bind ingFlags.NonPub lic)

    MsgBox(objField Info.GetValue(t ).ToString) ' returns "A"

    End Sub
    --

    Best regards,

    Carlos J. Quintero

    MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
    You can code, design and document much faster.
    Free resources for add-in developers:
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


    "rambalep" <wbrivio@gmail. com> escribió en el mensaje
    news:1130425619 .113123.277100@ g47g2000cwa.goo glegroups.com.. .[color=blue]
    >I need to get a class property value, where property name is contained
    > in a variable.
    >
    > example:
    > class test
    > dim p1 as string
    > dim p2 as string
    > end class
    > ...
    > sub x
    > dim t as test
    > dim myvar as string = "p1"
    > ...
    > 'I need to read a property of object t and the name of property is in
    > "myvar"
    > ?
    >
    > How can I do it?
    > Thanks
    >[/color]


    Comment

    • Larry Lard

      #3
      Re: Get class property value where name in variable


      rambalep wrote:[color=blue]
      > I need to get a class property value, where property name is contained
      > in a variable.
      >
      > example:
      > class test
      > dim p1 as string
      > dim p2 as string
      > end class
      > ...
      > sub x
      > dim t as test
      > dim myvar as string = "p1"
      > ...
      > 'I need to read a property of object t and the name of property is in
      > "myvar"[/color]

      Carlos has given you code to do this, but it's worth pointing out that
      if you find yourself having to use reflection in 'normal' code, it
      probably means your design is wrong.

      It's hard to tell from this barebones example but it looks like you
      want a String -> String Dictionary, as implemented for example by a
      simple Hashtable.

      --
      Larry Lard
      Replies to group please

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Get class property value where name in variable

        "rambalep" <wbrivio@gmail. com> schrieb:[color=blue]
        > 'I need to read a property of object t and the name of property is in
        > "myvar"[/color]

        => 'CallByName' function.

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

        Comment

        Working...