mardi 7 juin 2016

GetFields() or GetProperties()

First: Using VB.Net Okay, so Ive got a test program that is a mock of my much larger program. I can get the class type just fine at this point, but I need to be able to read all of the member available inside each class. After much reading on msdn I have come here for help. Should I be using GetFields() or GetProperties() or something else all together? I've tried both but neither are working the way I want and I have a good feeling it's because of the way I set my test program up. But anyways, here is code:

Imports System.Reflection

Module Module1

Sub Main()


    Dim Myself As New Human("Matthew", "Cucco", Now, "Blonde", 19, False)
    Dim NotMe As New Human("Jake", "Cucco", Now, "Blonde", 19, False)
    Dim Him As New Employee("Matt", "Cucco", Now, "Blonde", 19, False, 215, "LuK", True)
    Dim Her As New Customer("Jessie", "Keller", Now, "Blonde", 19, True, 25, "Cereal", "me@gmail.com")

    Dim ListofPeople As IList(Of Object) = {Myself, NotMe, Him, Her}
    Dim ListofPeople2 As IList(Of Object) = {Myself, NotMe, Him, Her}


    ObjectsAreSameClass(ListofPeople, ListofPeople2)
    Console.ReadKey()


End Sub

Private Function ObjectsAreSameClass(object1 As IList(Of Object), object2 As IList(Of Object)) As Boolean

    Dim ObjectType As Type = object1.First.GetType()
    Dim AreSameClass As Boolean = Nothing
    Console.WriteLine(ObjectType.ToString)

    If (object1.First.GetType() = object2.First.GetType()) Then
        AreSameClass = True
        Console.WriteLine("Object1 is of type: " + object1.First.GetType().Name)
        Console.WriteLine("Object2 is of type: " + object2.First.GetType().Name)
        If (object1.First.GetType().Name = "Human") Then
            Console.WriteLine("Yep this works")
        End If


    Else
        AreSameClass = False
        Console.WriteLine("Object1 is of type: " + object1.First.GetType().Name)
        Console.WriteLine("Object2 is of type: " + object2.First.GetType().Name)
        If (object1.First.GetType().Name = "Human") Then
            Console.WriteLine("Yep this works")
            Console.WriteLine(object1.First.GetType().GetFields().ToString)
        End If

    End If

    Dim MyField As PropertyInfo() = ObjectType.GetProperties()
    Dim i As Integer
    For i = 0 To MyField.Length - 1
        Console.WriteLine(MyField(i).ToString)
    Next i

    Console.WriteLine("Objects are equal? t/f : " + AreSameClass.ToString)

    Return AreSameClass
End Function


Public Class Human

    Public FirstName As String
    Public LastName As String
    Public Birthdate As Date
    Public HairColor As String
    Public Age As Integer
    Public Gender As Boolean   'False for male, true for female




    Public Sub New()

        FirstName = ""
        LastName = ""
        Birthdate = Now
        HairColor = ""
        Age = 0
        Gender = False

    End Sub

    Public Sub New(f As String, l As String, b As Date, h As String, a As Integer, g As Boolean)

        FirstName = f
        LastName = l
        Birthdate = b
        HairColor = h
        Age = a
        Gender = g

    End Sub

End Class


Public Class Employee
    Inherits Human

    Dim EmployeeId As Integer
    Dim PlaceOfEmployment As String
    Dim IsManager As Boolean

    Public Sub New()
        MyBase.New()
        EmployeeId = 0
        PlaceOfEmployment = ""
        IsManager = False
    End Sub

    Public Sub New(f As String, l As String, b As Date, h As String, a As Integer, g As Boolean, i As Integer, p As String, m As Boolean)
        MyBase.New(f, l, b, h, a, g)
        EmployeeId = i
        PlaceOfEmployment = p
        IsManager = m
    End Sub


End Class




Public Class Customer
    Inherits Human
    'used for testing

    Dim IdNumber As Integer
    Dim FavoriteItem As String
    Dim email As String
    Public Sub New()
        MyBase.New()
        IdNumber = 0
        FavoriteItem = ""
        email = ""
    End Sub

    Public Sub New(f As String, l As String, b As Date, h As String, a As Integer, g As Boolean, i As Integer, fav As String, e As String)
        MyBase.New(f, l, b, h, a, g)
        IdNumber = i
        FavoriteItem = fav
        email = e
    End Sub


End Class

End Module

Tried making all members public but that didn't fix the problem...





Aucun commentaire:

Enregistrer un commentaire