Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How To Call Into Parent Class
Message
From
12/06/2009 09:50:58
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01405105
Message ID:
01405484
Views:
42
Ooops...I agree.

Functionally, it works. But, not a best practice.

Thanks for pointing that out.




>Hi,
>Quibble:
>"ColumnCOllection.ParentUserControl = this;" won't work in your example unless ParentUserControl is made public (which is what Kevin wanted to avoid)
>
>
>>I also wanted to point out that you do not have to configure your constuctor to receive the reference unless you want it that way, because you can set it each time after instantiaion like this:
>>
>>Columns ColumnCollection = new Columns();
>>ColumnCOllection.ParentUserControl = this;
>>
>>Otherwise you will always have to pass in a reference, which could be null, I suppose.
>>
>>I've even seen it where you have two contsructors: one where you pass in the parent reference, and another constructor where you do not have to
>>
>>
>>public class Columns : CollectionBase
>>{
>>   Object ParentUserControl;
>>
>>   public Columns()  //-------- No parent parameter required
>>   {
>>     // ----regular code here
>>    }
>>
>>   public Columns(Object ParentUserControl) : this() // -------------------Accepts a paramter, and also call the regular constructor above
>>
>>   {
>>     this.parent = ParentUserControl;
>               ^ Should be this.ParentUserControl.....
>
>>    }
>>}
>>
>>
>>
>>Here's a link to some brief info on Constructor Chaining:
>>
>>http://en.csharp-online.net/Common_Type_System%E2%80%94Constructor_Chaining
>>
>>
>>
>>
>>>I had exactly this in my collection. What I didn't like about it is that since the collection is a public property, the reference to the parent is also public. But I guess it's not that big of a deal.
>>>
>>>Thanks Matt
>>>
>>>
>>>
>>>>There is no built-in Parent reference in C#, so, you need to design your Collection class to receive a reference to the parent that created it.
>>>>
>>>>Consider this sample I found on the web doing a Google search. Notice how the constructor in Worker receives a reference to the Game that created it. It is type dependent, so you will need to specify the object type that the Parent is expected to be. In the sample, the Worker class can then reference this.parent to reference the parent.
>>>>
>>>>It's called a 'reference' in C#. Get it? Here is a link to the place I found this sample: http://www.gamedev.net/community/forums/topic.asp?topic_id=493583
>>>>
>>>>and, here is another good example/explanation as well: http://stackoverflow.com/questions/185124/nested-class (talks about Nested Classes, but the idea of passing a parent reference in is still easy to see)
>>>>
>>>>Keep reading after the sample to see what I think YOUR class needs to look like... It should get you started in the right direction.
>>>>
>>>>Sample:
>>>>
>>>>public class Worker
>>>>{
>>>>  Game parent;
>>>>
>>>>  public Worker(Game game)
>>>>  {
>>>>    parent = game;
>>>>  }
>>>>}
>>>>
>>>>public class Game
>>>>{
>>>>  public void CreateRandomGameEnvironment()
>>>>  {
>>>>    for (int i=0; i<500; ++i)
>>>>    {
>>>>      Worker temp = new Worker(this);
>>>>      temp.Randomize(i);
>>>>      Workers.Add(temp);
>>>>    }
>>>>  }
>>>>}
>>>>
>>>>
>>>>So, your Collection class code might look like this:
>>>>
>>>>
>>>>public class Columns : CollectionBase
>>>>{
>>>>   Object ParentUserControl;   // ---------------- A new field needed to reference the parent (Need to change Object to you UserContol class here and in the constructor below
>>>>
>>>>   public Columns(Object ParentUserControl)   // ---------------- Add a constructor for your class that will receive a Parent reference
>>>>   {
>>>>     this.parent = ParentUserControl;
>>>>    }
>>>>
>>>>   public Column this[int index]
>>>>    {
>>>>        get {return (Column)List[index];}
>>>>    }
>>>>    public void Add(Column item)
>>>>    {
>>>>        List.Add(item);
>>>>        this.ParentUserControl.DoSomething();     //  -------------- Now you can reach the Parent to do things
>>>>    }
>>>>    public void Insert(int index, Column item)
>>>>    {
>>>>        List.Insert(index, item);
>>>>    }
>>>>    public void Remove(Column item)
>>>>    {
>>>>        List.Remove(item);
>>>>    }
>>>>}
>>>>
>>>>
>>>>
>>>>Also, when your UserControl creates an instance of this Collection, it will need to pass itself in:
>>>>
>>>>
>>>> Columns ColumnCollection  = new Columns(this);
>>>>
Previous
Reply
Map
View

Click here to load this message in the networking platform