Click here to Skip to main content
15,917,709 members
Home / Discussions / C#
   

C#

 
GeneralRe: Capitalize String in Grid Column Pin
Heath Stewart16-Apr-04 5:55
protectorHeath Stewart16-Apr-04 5:55 
GeneralRe: Capitalize String in Grid Column Pin
Bitwise Gamgee16-Apr-04 5:55
Bitwise Gamgee16-Apr-04 5:55 
GeneralRe: Capitalize String in Grid Column Pin
Heath Stewart16-Apr-04 6:10
protectorHeath Stewart16-Apr-04 6:10 
GeneralTreeView drag and drop Pin
Pete Burgess15-Apr-04 9:49
Pete Burgess15-Apr-04 9:49 
GeneralRe: TreeView drag and drop Pin
Heath Stewart15-Apr-04 11:18
protectorHeath Stewart15-Apr-04 11:18 
GeneralRe: TreeView drag and drop Pin
Pete Burgess15-Apr-04 11:35
Pete Burgess15-Apr-04 11:35 
GeneralRe: TreeView drag and drop Pin
Pete Burgess15-Apr-04 12:05
Pete Burgess15-Apr-04 12:05 
Questioncollections or methods? Pin
Judah Gabriel Himango15-Apr-04 9:39
sponsorJudah Gabriel Himango15-Apr-04 9:39 
A co-developer and I were in a discussion today about the right way to do this, I'd like some outside opinions on the subject.

Let's say we have a control that contains children of type Foo. Each child Foo can also contain children of type Foo.

To model the following, here's how I believe it should be modeled:

//////////// example #1, my solution ///////////////////
public class Foo
{

   private FooCollection children = new FooCollection();
   
   public Foo()
   {
      children.ItemAdded += SomeDelegate(this.ChildAddedCallback);
   }
   
   private void ChildAddedCallback(Foo addedChild)
   {
   	// do something special
   }
      
   public FooCollection Children
   {
      get
      {
      	return this.children;
      }
   }
}

<pre>public class FooCollection
{
   public event SomeDelegate ItemAdded;
   
   public FooCollection()
   {
   }
   
   public void Add(Foo item)
   {
   	if(this.ItemAdded != null)
   	   this.ItemAdded(item);
   }
}


// adding a child to the control
FooBarControl.Children.Add(foo);

// adding a child to the child
foo.Children.Add(anotherFoo);




The developer I was discussing this with disagreed with the above code. He instead believes it should look something like:

//////////// example #2, the other developer's solution ////////////////
public class Foo
{
   private FooCollection internalChildren = new FooCollection();
   
   public Foo()
   {
   }
   
   public void AddChild(Foo child)
   {
      internalChildren.Add(child);
      
      // do something special
   }
   
   public FooCollection GetChildren()
   {
   	FooCollection externalChildren = new FooCollection();
   	
   	foreach(Foo child in internalChildren)
   	   externalChildren.Add(child);
   	   
   	return externalChildren;
   }
}

<pre>public class FooCollection
{
   public class FooCollection()
   {
   }
   
   public void Add(Foo item)
   {
   }
}


// adding a child to the control
FooBarControl.AddChild(foo);

// adding a child to the child
foo.AddChild(anotherFoo); // note that Foo.AddChild is a different method than FooBarControl.AddChild




His argument was that the above code is object-oriented, and all the functionality for adding a child is contained in a single class.
His main argument was that my code (sample #1) would allow external objects to invalidate the inner workings of Foo children. With his solution (example #2), the inner workings of Foo would be completely concealed. His solution would protect the inner workings and inner data of the Foo children. His solution also gives more of a de-coupling of the collection-to-Foo.

My argument was that my solution (example #1) follows that of the .NET FCL (look at TreeView w/ TreeNode for example, or any other control with children/items). I also argued that my solution is truely object oriented, using a seperate object to store and manipulate the children.
Additionally, my solution would allow for code reuse, in that the main FooBar control containing Foo items can also utilize the collection, without the need to write .AddChild methods for both the FooBar control and Foo item.

So, I'd like to hear from you all on how you guys would implement such a structure. Is example #1 or example #2 closer to what you would do/recommend?


---------------------------
He who knows that enough is enough will always have enough.

-Lao Tsu

AnswerRe: collections or methods? Pin
Heath Stewart15-Apr-04 9:59
protectorHeath Stewart15-Apr-04 9:59 
GeneralRe: collections or methods? Pin
Judah Gabriel Himango15-Apr-04 10:22
sponsorJudah Gabriel Himango15-Apr-04 10:22 
GeneralRe: collections or methods? Pin
Heath Stewart15-Apr-04 10:51
protectorHeath Stewart15-Apr-04 10:51 
GeneralRe: collections or methods? Pin
Judah Gabriel Himango15-Apr-04 11:23
sponsorJudah Gabriel Himango15-Apr-04 11:23 
GeneralRe: collections or methods? Pin
Steven Campbell15-Apr-04 11:17
Steven Campbell15-Apr-04 11:17 
GeneralRe: collections or methods? Pin
Judah Gabriel Himango15-Apr-04 16:41
sponsorJudah Gabriel Himango15-Apr-04 16:41 
AnswerRe: collections or methods? Pin
scadaguy15-Apr-04 11:42
scadaguy15-Apr-04 11:42 
GeneralRe: collections or methods? Pin
Judah Gabriel Himango15-Apr-04 16:53
sponsorJudah Gabriel Himango15-Apr-04 16:53 
AnswerRe: collections or methods? Pin
bjoernen24-Apr-04 23:06
bjoernen24-Apr-04 23:06 
GeneralHotkeys Pin
rickrf15-Apr-04 7:43
rickrf15-Apr-04 7:43 
GeneralRe: Hotkeys Pin
Tom Clement15-Apr-04 7:51
professionalTom Clement15-Apr-04 7:51 
GeneralRe: Hotkeys Pin
Heath Stewart15-Apr-04 8:41
protectorHeath Stewart15-Apr-04 8:41 
GeneralRe: Hotkeys Pin
Mazdak15-Apr-04 9:24
Mazdak15-Apr-04 9:24 
GeneralRe: Hotkeys Pin
Heath Stewart15-Apr-04 9:25
protectorHeath Stewart15-Apr-04 9:25 
GeneralRe: Hotkeys Pin
rickrf16-Apr-04 4:36
rickrf16-Apr-04 4:36 
GeneralRe: Hotkeys Pin
rickrf16-Apr-04 5:16
rickrf16-Apr-04 5:16 
GeneralRe: Hotkeys Pin
Bee Master16-Apr-04 17:46
Bee Master16-Apr-04 17:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.