|
-
Aug 31st, 2012, 01:33 AM
#1
[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!
-
Aug 31st, 2012, 03:04 AM
#2
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:
Dim sourceType As Type = Test.GetType()
Dim field As FieldInfo = sourceType.GetField(MemberVariableName)
If field IsNot Nothing Then
Dim value As Object = field.GetValue(Test)
'...
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.
-
Aug 31st, 2012, 03:56 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|