Diesel Posted July 26, 2004 Posted July 26, 2004 I have a class in which I create two objects, one I fill with data and the other is a copy of the first. Im using reflection to get the public properties of each class... Dim myType As Type = GetType(ECSolutions.Inventory.Vehicle) ' Get the public properties. Dim myPropertyInfo As System.Reflection.PropertyInfo() = Me.GetType.GetProperties((BindingFlags.Public Or BindingFlags.Instance)) Dim hisPropertyInfo As PropertyInfo() = obj.GetType.GetProperties((BindingFlags.Public Or BindingFlags.Instance)) But now, im trying to compare the actual values in the properties with no luck...any help? For Each prop In myPropertyInfo If CType(prop.GetValue(prop, i), String) <> CType(hisPropertyInfo(i).GetValue, String) Then Return 0 i += 1 End If Next Quote
Joe Mamma Posted July 26, 2004 Posted July 26, 2004 back up. . . Look at implementing IComparable, or overriding the Equals method on your contained class. Drop the old VB way of doing things!!!! CType is not .NET Iterating through the properties is procedural. At runtime a class instance should know who to compare itself to other objects. ala: If class1.CompareTo(class2) <> 0 then DoSomething end if or If not class1.Equals(class2) then DoSomething end if though overriding Equals is probably not advised except in the most basic of situations due to side effects. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Diesel Posted July 27, 2004 Author Posted July 27, 2004 Really? I am using IComparable...the above code was the implementation inside CompareTo. Im not comparing a class though, Im comparing objects. I want to make sure the data in the objects is exactly the same. I think I found something in propertydescriptioncollection. What are the side effects of overriding Equals? Quote
Administrators PlausiblyDamp Posted July 27, 2004 Administrators Posted July 27, 2004 If you are implementing IComparable then you may as well just compare the fields yourself - using reflection will cause a performance hit for something as simple as this. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Diesel Posted July 27, 2004 Author Posted July 27, 2004 Oh IC Ok, then, I'll just do it manually. I guess I was just trying to make it flexible, but your right, it's probably not worth it. Quote
Diesel Posted July 27, 2004 Author Posted July 27, 2004 Wait a sec... Ctype is not .Net? It is a checked function, check the declaration...How else can you typecast in VB.net? Quote
Administrators PlausiblyDamp Posted July 27, 2004 Administrators Posted July 27, 2004 Depends what you are converting between, to convert to a string all objects will implement a .ToString() method. Other data type often have their own method i.e. Integer.Parse() to convert a string to an integer. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Diesel Posted July 27, 2004 Author Posted July 27, 2004 What about... What about converting one object to another...say a parent class to a subclass Say you have a function that takes a superclass as a parameter, but you want to use a property of the subclass.... Public Function PrintAttributes(obj as Person) as String Dim str as string str = obj.name str &= obj.age str &= Ctype(obj, Employee).jobtitle End Function Quote
Administrators PlausiblyDamp Posted July 27, 2004 Administrators Posted July 27, 2004 In that case use DirectCast instead of CType Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.