By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,579 Members | 1,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,579 IT Pros & Developers. It's quick & easy.

Property of List of Classes

P: n/a
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?

Feb 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
news:11*********************@g47g2000cwa.googlegro ups.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?

Feb 16 '06 #2

P: n/a
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!

Feb 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.