Home All Groups Group Topic Archive Search About

Property of List of Classes

Author
16 Feb 2006 3:44 PM
zacks
I asked this question before but got no responses. Let me rephrase the
question. I have a class that as a property defined as a list of
classes.

Public Class ClassA

Public Property Prop1 as List(of ClassB)

In a code segment, I can add an instance of ClassB to the Prop1
property of an instance of ClassA with the statement:

myClassA.Prop1.Add myClassB

Can I add an instance of ClassB to the Prop1 List using the
..InvokeMember method of the Type class?

Author
16 Feb 2006 4:29 PM
Carlos J. Quintero [VB MVP]
Try:

Public Class ClassB

End Class

Public Class ClassA
   Public Prop1 As New List(Of ClassB)
End Class

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

   Dim objA As New ClassA
   Dim objArray(0) As ClassB

   objA.Prop1.Add(New ClassB)

   objArray(0) = New ClassB
   objA.Prop1.GetType.InvokeMember("Add", Reflection.BindingFlags.Instance
Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.InvokeMethod,
Nothing, objA.Prop1, objArray)

   MessageBox.Show(objA.Prop1.Count.ToString) ' Result: 2

End Sub

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


<za***@construction-imaging.com> escribió en el mensaje
Show quoteHide quote
news:1140104688.906608.16700@g47g2000cwa.googlegroups.com...
>I asked this question before but got no responses. Let me rephrase the
> question. I have a class that as a property defined as a list of
> classes.
>
> Public Class ClassA
>
> Public Property Prop1 as List(of ClassB)
>
> In a code segment, I can add an instance of ClassB to the Prop1
> property of an instance of ClassA with the statement:
>
> myClassA.Prop1.Add myClassB
>
> Can I add an instance of ClassB to the Prop1 List using the
> .InvokeMember method of the Type class?
>
Author
16 Feb 2006 9:12 PM
zacks
Carlos J. Quintero [VB MVP] wrote:
>    objArray(0)    objA.Prop1.GetType.InvokeMember("Add", Reflection.BindingFlags.Instance
> Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.InvokeMethod,
> Nothing, objA.Prop1, objArray)

Oh, man, I was thinking the only way to set a property value in
InvokeMethod was to use the bindings flag of SetProperty. I never
thought of Invoking the Add method!! Very elegant. I will try your
suggestion!