Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

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.

Posted

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?

Posted

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.

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...