How to support multiple objects sharing same List propery

B

BobRoyAce

Let's say that I have a class, called EntityNote. Then, suppose that I
have multiple classes, all of which have a property, called Notes,
which is a List(Of EntityNote). One of the methods of each of these
classes is called AddNoteTo, and it is, as you'd expect, used to add a
new EntityNote to Notes.

Now, what I want to do is have one form that can be used to view
existing EntityNotes, and add new ones, for all of the classes that
have a Notes property. What I'd like to do is have a property of the
form that I could assign the reference to. However, how can I have a
property like that, where I would need to be able to assign instances
of many different classes to it (one at a time, of course). For
example, one time I may want to open the form showing Notes for a
Class1, and be able to call Class1.AddNoteTo method. Then, I may want
to open the same form again, this time showing all Notes for a Class2,
and be able to call Class2.AddNoteTo method. And, so on...

How could I accomplish something like this?
 
A

Armin Zingler

BobRoyAce said:
Let's say that I have a class, called EntityNote. Then, suppose that I
have multiple classes, all of which have a property, called Notes,
which is a List(Of EntityNote). One of the methods of each of these
classes is called AddNoteTo, and it is, as you'd expect, used to add a
new EntityNote to Notes.

Now, what I want to do is have one form that can be used to view
existing EntityNotes, and add new ones, for all of the classes that
have a Notes property. What I'd like to do is have a property of the
form that I could assign the reference to. However, how can I have a
property like that, where I would need to be able to assign instances
of many different classes to it (one at a time, of course). For
example, one time I may want to open the form showing Notes for a
Class1, and be able to call Class1.AddNoteTo method. Then, I may want
to open the same form again, this time showing all Notes for a Class2,
and be able to call Class2.AddNoteTo method. And, so on...

How could I accomplish something like this?


Put the code that Class1 and Class2 have in common (property Notes, method
AddNoteTo) in a base class and make Class1 and Class2 inherit from it. Then
the type of the Form property can be the base class of Class1 and Class2.

(Is there a reason why you need an addition AddNoteTo method? The Notes
property returns a List that has an Add method already.)


Armin
 
B

BobRoyAce

Thanks...will try to implement that.

You are quite right about the List.Add method. A couple of reasons why
one might want to have the AddNoteTo method anyway are:
1) May not want to give direct WRITE access to the Notes property
(i.e. READONLY).
2) May have additional processing associated with adding a note
(e.g. validation of some sort).
 
A

Armin Zingler

BobRoyAce said:
Thanks...will try to implement that.

You are quite right about the List.Add method. A couple of reasons why
one might want to have the AddNoteTo method anyway are:
1) May not want to give direct WRITE access to the Notes property
(i.e. READONLY).

What is readonly? The Notes property? That only prevents the Notes property
from being changed (object.Notes = New List(Of...) wasn't possible). One
could still call object.Notes.Add.
2) May have additional processing associated with adding a note
(e.g. validation of some sort).

object.Notes.Add would bypass the validation process. If validation is
limited to the List object itself, I'd write my own list class and keep the
entries in an internal List(Of EntityNote). In the Add method you can do the
validation. (Even if it was less work, I'd not derive from List(Of
EntityNote) because shadowing the Add method only gives "security through
obscurity" - one could still access the base class' Add method.)



Armin
 
P

Phill W.

BobRoyAce said:
Let's say that I have a class, called EntityNote. Then, suppose that I
have multiple classes, all of which have a property, called Notes,
which is a List(Of EntityNote). One of the methods of each of these
classes is called AddNoteTo, and it is, as you'd expect, used to add a
new EntityNote to Notes.

I'd implement this slightly differently.

Class Class1
Public ReadOnly Property Notes() as ListOfEntityNote
Get
Return m_notes
End Get
[?Friend?] Set( value as ListOfEntityNote )
m_notes = Value
End Set
End Property

Private m_notes as New ListOfEntityNote

End Class

You'd probably want the "set Notes" to be less accessible than "get Notes".

Then, the "collection" class, ListOfEntityNote, would handle all the
things that need to be "done" to that "collection":

Class ListOfEntityNote
Inherits List(Of EntityNote)

' If you want to intercept additions, you may have to
' shadow the existing Add method.
Public [?Shadows?] Sub Add(ByVal entry as EntityNote)
If m_bReadOnly Then Throw New Exception( ...

Me.Validate( entry )
MyBase.Add( entry )
End Sub

Public Sub Add(ByVal sText as String)

MyBase.Add( New EntityNote( sText ) )
End Sub

Public ReadOnly Property ReadOnly() as Boolean
Get
Return m_bReadOnly
End Get
End Property

Friend Sub MakeReadOnly()
m_bReadOnly = True
End Sub

Private m_bReadOnly as Boolean = False

Private Sub Validate( ByVal entry as EntityNote )
If [?Something Dodgy?] Then
Throw New Exception( ...
End If
End Sub

End Class
one time I may want to open the form showing Notes for a
Class1, and be able to call Class1.AddNoteTo method. Then, I may want
to open the same form again, this time showing all Notes for a Class2,
and be able to call Class2.AddNoteTo method. And, so on...

Sounds like you want a Form that can display the contents of any given
ListOfEntityNote object ...

HTH,
Phill W.
 
M

Mr. Arnold

BobRoyAce said:
Let's say that I have a class, called EntityNote. Then, suppose that I
have multiple classes, all of which have a property, called Notes,
which is a List(Of EntityNote). One of the methods of each of these
classes is called AddNoteTo, and it is, as you'd expect, used to add a
new EntityNote to Notes.

Now, what I want to do is have one form that can be used to view
existing EntityNotes, and add new ones, for all of the classes that
have a Notes property. What I'd like to do is have a property of the
form that I could assign the reference to. However, how can I have a
property like that, where I would need to be able to assign instances
of many different classes to it (one at a time, of course). For
example, one time I may want to open the form showing Notes for a
Class1, and be able to call Class1.AddNoteTo method. Then, I may want
to open the same form again, this time showing all Notes for a Class2,
and be able to call Class2.AddNoteTo method. And, so on...

How could I accomplish something like this?


You have to have a field in all the objects. That field would designate
what "type" of object it is.

List<Note>

List<Note> contains all the different types of Note entities.

You should be able to use LINQ and query against "EntityNotes" to pull
the objects wanted based on a type.

base.Note and all other Note entities are derived from base.Note, which
is called 'Inheritance' and base.Note has the 'Type' field.

List<Note> entitynotes = new List<Note>()

Note note = new Note()

note.Type = 1

entitynotes.Add(note)


You should think OOP and not Class.


What is Object-oriented-programming?

(OOP) is a programming paradigm that uses "objects" and their
interactions to design applications and computer programs.

The key concepts of OOP are the following:

Class
Object
Instance
Method
Message passing
Inheritance
Abstraction
Encapsulation
Polymorphism
Decoupling

http://en.wikipedia.org/wiki/Object-oriented_programming
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top