Results 1 to 3 of 3

Thread: [RESOLVED] HowTo Use the Name of Structur-Member Variable as an argument for a Function

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [RESOLVED] HowTo Use the Name of Structur-Member Variable as an argument for a Function

    For example:
    Code:
    Public Structure MyStructure
       Public Int1 As Integer
       Public Int2 As Integer
       Public Int3 As Integer
       Public Int4 As Integer
    End Structure
    ...
    ....
    
    Dim Test as New MyStructure
    ...
    'Somewhere in Code
    DoCheck("Int1")
    DoCheck("Int2")
    DoCheck("Int3")
    DoCheck("Int4")
    ....
    ....
    Public Sub DoCheck(MemberVariableName as String)
       If Test.MemberVariableName=Whatever Then
       End If
    End Sub
    I know it doesn't work like this, but is it possible to handover the name of a membervariable and use it like that?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HowTo Use the Name of Structur-Member Variable as an argument for a Function

    It's possible but it should generally be avoided if there's another way to do it. You would have to use Reflection in your DoCheck method:
    vb.net Code:
    1. Dim sourceType As Type = Test.GetType()
    2. Dim field As FieldInfo = sourceType.GetField(MemberVariableName)
    3.  
    4. If field IsNot Nothing Then
    5.     Dim value As Object = field.GetValue(Test)
    6.  
    7.     '...
    8. End If
    Reflection is useful in certain scenarios but is slow to execute and cumbersome to work with in code, hence it should only be used where there is no other convenient way to achieve your aim. Even if it requires a bit more code to do it without Reflection, you should take the other option. If it takes a lot more code or it's just not possible, then Reflection is a good option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: HowTo Use the Name of Structur-Member Variable as an argument for a Function

    ..understood.
    I'll take the option "a bit more code without reflection"

    Thanks for the advice!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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