Re: Using Type Class

From: Dennis (Dennis_at_discussions.microsoft.com)
Date: 10/18/04


Date: Mon, 18 Oct 2004 16:59:03 -0700

I tried the PropertyDescriptor route and I found two things;

1) I had to add ".GeType.ToString" in use dim c as type =
type.GetType(myarraylist(0).GetType.ToString) to get it to compile

2) I get an error when it tries to sort in the
x=m_Property.GetValue(propertyName) that just says unhandled exception in
Icompare.

Any idea what is going on?

"Jay B. Harlow [MVP - Outlook]" wrote:

> Dennis,
> > The major drawback, I think, is speed.
> Which is where I would consider using a PropertyDescriptor instead of
> CallByName in this case...
>
> Something like:
>
> Protected Class mycompare
> Implements IComparer
>
> Private ReadOnly m_property As
> System.ComponentModel.PropertyDescriptor
> Private ReadOnly m_ascending As Boolean
>
> Public Sub New(ByVal componentType As Type, ByVal propertyName As
> String, ByVal ascending As Boolean)
> Dim properties As
> System.ComponentModel.PropertyDescriptorCollection
> properties =
> System.ComponentModel.TypeDescriptor.GetProperties(componentType)
> m_property = properties(propertyName)
> m_ascending = ascending
> End Sub
>
> Private Function Compare(ByVal x As Object, ByVal y As Object) As
> Integer Implements IComparer.Compare
> x = m_property.GetValue(x)
> y = m_property.GetValue(y)
> If m_ascending Then
> Return New CaseInsensitiveComparer().Compare(x, y)
> Else
> Return New CaseInsensitiveComparer().Compare(y, x)
> End If
> End Function
>
> End Class
>
> >> >> > dim C as type = type.GetType(myarraylist(0))
> Dim mycomparer as new mycombare(C, col, True)
> >> > myArrayList.Sort(mycomparer)
>
> Hope this helps
> Jay
>
> "Dennis" <Dennis@discussions.microsoft.com> wrote in message
> news:716388DE-A3C4-410B-9EBF-F5CD5E8B3430@microsoft.com...
> > You are, of course as usual, correct. I was just trying to simpify my
> > explaination. The major drawback, I think, is speed. I probably would be
> > faster to use the object with properties directly and use late binding
> > rather
> > than CallbyName. But anyway, it's pretty fast for small arraylists,
> > arrays,
> > etc. Thanks again for your help.
> >
> > "Jay B. Harlow [MVP - Outlook]" wrote:
> >
> >> Dennis,
> >> Actually your code will work on two objects of different types as long as
> >> they have the same property names.
> >>
> >> For example, your class will compare the these two classes without any
> >> problems:
> >>
> >> Public Class Class1
> >>
> >> Public ReadOnly Property Test() As String
> >> ...
> >>
> >> End Class
> >>
> >> Public Class Class2
> >>
> >> Public ReadOnly Property Test() As String
> >> ...
> >>
> >> End Class
> >>
> >> Jay
> >>
> >>
> >> "Dennis" <Dennis@discussions.microsoft.com> wrote in message
> >> news:E1ECADB9-EA69-42FC-8395-DC12E5988855@microsoft.com...
> >> > Thanks Jay and Cor. I was thinking about the CallbyName but I didn't
> >> > know
> >> > it
> >> > would work with a general arraylist of classes. Following is what I
> >> > ended
> >> > up
> >> > with that will sort any arraylist of classes (must be same class types)
> >> > by
> >> > any string or integer or decimal property of the class in ascending or
> >> > descending order:
> >> >
> >> > dim myArrayList as ArrayList
> >> > 'Set each element of the ArrayList to any class (must all be of same
> >> > type)
> >> > Dim mycomparer As New mycompare
> >> >
> >> > mycomparer.propertyname = col
> >> > mycomparer.ascending = True
> >> > myArrayList.Sort(mycomparer)
> >> > .......
> >> > .......
> >> > __________________________________________________________________
> >> > Protected Class mycompare
> >> > Implements IComparer
> >> > Public propertyname As String
> >> > Public ascending As Boolean
> >> > Private Function Compare(ByVal x As Object, ByVal y As Object) As
> >> > Integer
> >> > _ Implements IComparer.Compare
> >> > If ascending Then
> >> > Return New CaseInsensitiveComparer().Compare(CallByName(x, _
> >> > propertyname, CallType.Get), CallByName(y, propertyname, _
> >> > CallType.Get))
> >> > Else
> >> > Return New CaseInsensitiveComparer().Compare(CallByName(y, _
> >> > propertyname, CallType.Get), CallByName(x, propertyname, _
> >> > CallType.Get))
> >> > End If
> >> > End Function
> >> > End Class
> >> > _______________________________________________________________
> >> >
> >> > "Jay B. Harlow [MVP - Outlook]" wrote:
> >> >
> >> >> Dennis,
> >> >> > dim C as type = type.GetType(myarraylist(0))
> >> >> > b = DirectCast(myarraylist(0), C ).MyProperty
> >> >>
> >> >> You cannot cast based on a Type variable, the DirectCast needs the
> >> >> name
> >> >> (myclass) of the type at compile time.
> >> >>
> >> >> If you truly want to get AnyProperty form an ArrayList of AnyClass,
> >> >> the
> >> >> "easist" way is to use CallByName
> >> >>
> >> >> Dim o As Object = CallByName(list(0), "MyProperty", CallType.Get)
> >> >>
> >> >>
> >> >> Alternatively I will use a PropertyDescriptor or Reflection.
> >> >>
> >> >> Something like:
> >> >>
> >> >> Dim properties As
> >> >> System.ComponentModel.PropertyDescriptorCollection
> >> >> properties =
> >> >> System.ComponentModel.TypeDescriptor.GetProperties(list(0))
> >> >>
> >> >> Dim o As Object = properties("MyProperty").GetValue(list(0))
> >> >>
> >> >> However I normally only use PropertyDescriptor in more advanced
> >> >> cases...
> >> >>
> >> >> Hope this helps
> >> >> Jay
> >> >>
> >> >> "Dennis" <Dennis@discussions.microsoft.com> wrote in message
> >> >> news:1F99CE53-992E-4895-8626-63CF63E632CF@microsoft.com...
> >> >> >I have a class named "myclass" and an arraylist containing elements
> >> >> >of
> >> >> >type
> >> >> > "MyClass". I want to get the value of a property of "MyClass" (a
> >> >> > string
> >> >> > type) for one of the arraylist elements.
> >> >> >
> >> >> > I can get this using:
> >> >> > dim b as string
> >> >> > b = DirectCast(myarraylist(0),myclass).myproperty
> >> >> >
> >> >> > However, I want to use an object to define the type "MyClass" like:
> >> >> > dim C as type = type.GetType(myarraylist(0))
> >> >> > b = DirectCast(myarraylist(0), C ).MyProperty
> >> >> >
> >> >> > but I get a "c" type not defined. How can I do this?
> >> >> >
> >> >> > Thanks for any help.
> >> >> >
> >> >> >
> >> >> > --
> >> >> > Dennis in Houston
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>



Relevant Pages

  • Re: Using Type Class
    ... > The major drawback, I think, is speed. ... Which is where I would consider using a PropertyDescriptor instead of ... CallByName in this case... ... >> Public Class Class1 ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Using Type Class
    ... Propertydescriptor) and see how much faster the PropertyDescriptor is. ... >> The major drawback, I think, is speed. ... > CallByName in this case... ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Using Type Class
    ... that the PropertyDescriptor solution requires that all the ... classes be the same type (it can be a common base ... >> Protected Class mycompare ...
    (microsoft.public.dotnet.languages.vb)