Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How To Call Into Parent Class
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01405105
Message ID:
01405273
Views:
37
The reference doesn't need to be public (it's not in the example)

>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