MattCucco MattCucco - 1 year ago 192 Question

GetFields Operator

I'm using a function to get the field data from a list. I need to know if the .GetFields operator will just return the fields, or if it will actually populate them with the data stored there? I think it's the latter after reading on msdn but I have no clue and I've never used these "test methods" before :(. Any help is appreciated! (or even if you can tell me how to do a test method would help!)
Here is code:

''' <summary>
''' This function will return all of the fields for a certain class as well as the data stored in them
''' </summary>
''' <param name="list"></param>
''' <returns></returns>
Public Shared Function GetFieldData(ByVal list As IList(Of Object)) As FieldInfo()

Dim fields() As FieldInfo = list.Single.GetType().GetFields()

Return fields

End Function

End Class

Here is code for creating newitems

''' <summary>
''' This function will create new Before and After objects
''' everything should be passed in as a IEnum
''' </summary>
''' <param name="Before"></param>
''' <param name="After"></param>
''' <returns></returns>
Function NewItem(Before As IEnumerable(Of Object), After As IEnumerable(Of Object))

If (Not ObjectsAreSameClass(Before, After)) Then 'If object classes are not the same, send an error message, else continue
'Pop error


Dim BeforeFields() As FieldInfo = GetFieldData(Before)

Dim AfterFields() As FieldInfo = GetFieldData(After)

ObjectCounter += 1

'Now check and make sure the objects are not the same
If (BeforeFields.Equals(AfterFields)) Then
'Objects are the same so pop error?

End If

End If

Return Nothing
End Function

Answer Source

FieldInfo is information about the field, not including its value. To get the value you have to provide an instance of that object type. Here's an example that you can place on a form to see how it works:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim info() As FieldInfo = GetType(Form).GetFields(BindingFlags.NonPublic Or 
    For Each item As FieldInfo In info
        Dim value As Object = item.GetValue(Me) ' Get the value from 'Me'
        If Not IsNothing(value) Then
            Debug.Print("{0} = {1}", item.Name, value.ToString())
            Debug.Print("{0} = Nothing", item.Name)
        End If
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download