473,413 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Using Type Class

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
Nov 21 '05 #1
16 1879
"Dennis" <De****@discussions.microsoft.com> schrieb:
I have a class named "myclass" and an arraylist
containing elements of type "MyClass".
Notice that 'MyClass' is a keyword in the VB.NET programming language.
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


\\\
If TypeOf myarraylist(0) Is Foo Then
b = DirectCast(myarraylist(0), Foo).MyProperty
ElseIf...Then
...
....
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
Dennis,

I cannot offer you much, however this will work
\\\
Public Class Example
Public Shared Sub Main()
Dim myarraylist As New ArrayList
Dim mywhatever As New WindowsApplication1.myclass
myarraylist.Add(mywhatever)
Dim b As String = DirectCast(myarraylist(0), _
WindowsApplication1.myclass).myproperty
If TypeOf myarraylist(0) Is WindowsApplication1.myclass Then
MessageBox.Show(DirectCast(myarraylist(0), _
WindowsApplication1.myclass).myproperty)
End If
End Sub
End Class
Public Class [myclass]
Public myproperty As String
Public Sub New()
myproperty = "Hello World"
End Sub
End Class
///

I would not use that myclass because it is very confusing because it is a
keyword in VBNet.
(And I used an very easy way for a property only to keep it short)

I hope this helps anyway a little bit?

Cor

"Dennis" <De****@discussions.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

Nov 21 '05 #3
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" <De****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.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

Nov 21 '05 #4
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" <De****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.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


Nov 21 '05 #5
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" <De****@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.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


Nov 21 '05 #6
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" <De****@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.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


Nov 21 '05 #7
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" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> news:1F**********************************@microsof t.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
>>
>>
>>


Nov 21 '05 #8
Dennis,

I hope you saw that my example was early binding.

It depends how many classes you use however in my idea can it be an easy
approach.

I can me not immaging a situation where you do not know what kind of objects
you add to your arraylist so it should be as well be possible to collect
them in that way of my sample back.

(And than of course in a If ElseIf piece of code)

I hope this helps?

Cor
Nov 21 '05 #9
I am working on an application for my personal use that uses several
arraylists each of which has differing classes (some of which I haven't even
designed yet) and I want to use a class that inherits a DataGrid which sorts
on the click of a button by different properties (columns) in different
arraylists.

I can then click on the datagrid's column header and sort the arrarylists no
matter which arraylist to which the DataGrid is bound. It works great with
the CallByName but I read where this can be slow in a loop, of which, sorting
is a loop(s).

"Cor Ligthert" wrote:
Dennis,

I hope you saw that my example was early binding.

It depends how many classes you use however in my idea can it be an easy
approach.

I can me not immaging a situation where you do not know what kind of objects
you add to your arraylist so it should be as well be possible to collect
them in that way of my sample back.

(And than of course in a If ElseIf piece of code)

I hope this helps?

Cor

Nov 21 '05 #10
Thanks a lot. I'll try the two different methods (callbyname and
Propertydescriptor) and see how much faster the PropertyDescriptor is. Just
curious as to why you made the m_Ascending a ReadOnly since it's private
anyway and can't be accessed outside the class.

"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" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> news:1F**********************************@microsof t.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
>>
>>
>>


Nov 21 '05 #11
Dennis,
I make member fields Read-only when the value of the field is not suppose to
change outside the Constructor (sub new) it keeps me honest when I am
writing the class, plus it keeps myself & subsequent programmers honest who
may modify the class later. Hopefully if they attempt to modify the field
and see it Read-only, then they will think a minute & research why its
read-only...

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
Thanks a lot. I'll try the two different methods (callbyname and
Propertydescriptor) and see how much faster the PropertyDescriptor is.
Just
curious as to why you made the m_Ascending a ReadOnly since it's private
anyway and can't be accessed outside the class.

"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" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> >> news:1F**********************************@microsof t.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
>> >>
>> >>
>> >>
>>
>>
>>


Nov 21 '05 #12
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" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> news:1F**********************************@microsof t.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
>>
>>
>>


Nov 21 '05 #13
Dennis,
I suspect #2 is caused by #1.

I copied #1 from your original post. It should read:

Dim c As Type = myarraylist(0).GetType()

Or perferable:
Dim c As Type = GetType(AnyClass)

Where AnyClass is the class name of the objects you are putting into the
ArrayList.

Something like:

Public Class Class1

Public ReadOnly m_test As String

Public Sub New(ByVal test As String)
m_test = test
End Sub
Public ReadOnly Property Test() As String
Get
Return m_test
End Get
End Property

End Class

Public Shared Sub Main()
Dim myarraylist As New ArrayList
myarraylist.Add(New Class1("A"))
myarraylist.Add(New Class1("B"))
myarraylist.Add(New Class1("C"))

Const col As String = "Test"

Dim C As Type = GetType(Class1)

Dim mycomparer As New mycompare(C, col, True)
myarraylist.Sort(mycomparer)

Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New MainForm)
End Sub

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:80**********************************@microsof t.com...
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" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> >> news:1F**********************************@microsof t.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
>> >>
>> >>
>> >>
>>
>>
>>


Nov 21 '05 #14
Dennis,

And why can you than not use my solution?

Unlucky it can not in a select case because then it would be even more easy
to use.

Cor
Nov 21 '05 #15
Dennis,
I should add, that the PropertyDescriptor solution requires that all the
classes (in a single ArrayList) be the same type (it can be a common base
type).

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:80**********************************@microsof t.com...
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" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> >> news:1F**********************************@microsof t.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
>> >>
>> >>
>> >>
>>
>>
>>


Nov 21 '05 #16
Jay, I got it working. You'll never believe what the real problem was. I
was passing the property name as I had specified it which was "Col1". The
propertyDescriptor converted it to lower case, i.e., "col1". When I passed
the lower case to the Icomparer routine, it works great. Thanks a lot for
you help.

"Jay B. Harlow [MVP - Outlook]" wrote:
Dennis,
I should add, that the PropertyDescriptor solution requires that all the
classes (in a single ArrayList) be the same type (it can be a common base
type).

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:80**********************************@microsof t.com...
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" <De****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> news:E1**********************************@microsof t.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" <De****@discussions.microsoft.com> wrote in message
>> >> news:1F**********************************@microsof t.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
>> >>
>> >>
>> >>
>>
>>
>>


Nov 21 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
2
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object,...
7
by: adam | last post by:
i'm working on a portion of a CMS that allows content-admins to browse a product list, and add individual products into the taxonomy by clicking checkboxes next to categories they might belong in....
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
1
by: sourcie | last post by:
I am changing an existing quiz found on "JavaScriptKit.com Multiple Choice Quiz" I have an image. Instead of using the radio buttons with the normal true/false question, I want to place two...
8
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.