Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can somebody please tell me the best solution to the following

Is there a way to retrieve a value from a parent class such as the following...
C#
public class MyGrid
{
    public List<Column> Columns = new List<Column>();
    private int DefaultColumnWidth = 64;
}

public class Column
{
    public Column()
    {
        Width = -1;
    }

    public int Width
    {
        get
        {
            if (Width >= 0)
                return Width;
            else
                // return DefaultColumnWidth from parent class
                return base.DefaultColumnWidth;
        }
        set;
    }
}


I want it so that if the DefaultColumnWidth changes, then the columns will reflect the change.

Is the best solution to...
Pass the DefaultColumnWidth as a reference
Somehow reference the parent class into a variable
Get the column class to return what value it has, and get the parent class to check if it is less than zero, and if true, use the DefaultColumnWidth

Spent hours reading about this, but not sure which path is the best (or even possible)

Thank you.
Posted
Comments
BillWoodruff 26-Sep-13 9:57am    
I am curious to ask you if it is your intent to expose the Class 'Column "publicly" (which is what you are doing now) ? I believe that what you may want here is for the 'Column Class to be encapsulated (nested) in the 'MyGrid Class, and not accessible outside instances of the 'MyGrid Class.

In my opinion having 'Column inherit from 'MyGrid ... while it does work ... is not the right choice, or, best practice from an OOP perspective. Inheritance implies an "is-a" relationship, and it's not the case that the 'Column Class "is-a" 'MyGrid Class. It is the case that an instance of the 'MyGrid Class "holds" a bunch of instances of the 'Column Class in a generic LIst. In OOP terms: 'MyGrid "has-a" bunch of 'Column.

There is a common design problem in .NET inherent in nesting Classes to control "visibility" that many people have trouble with (there's no automatic access in a nested Class to the Class it is defined in, as there is in other languages, like Java); there's no equivalent to what .NET gives you in using Controls where you can get the Container Control the Control is sited in using the Control's 'Parent property.

If you are satisfied with the solution you have now, that's great; if you want to hear more, just let me know.

In my perception there is a serious structural problem in your code as is: once you have set a Column's width to a value equal to the 'DefaultCommentWidth: you have "lost" the information about whether it is your intent for that Column to be updated every time 'DefaultCommentWidth changes, or, to keep an "independent" value.

If you want to hear more about this, and what I think is a possible change in architecture so each instance of 'Column can either synchronize with 'DefaultColumnWidth, or, keep its value "independently," just ask.
Grant Mc 27-Sep-13 3:44am    
Hi Bill, I would love to see what you think what would work better.

It would be great to see how how to synchronize the values, even if I don't use it for this project. Just to learn what can be done

Thanks.
Grant

public class MyGrid
   {
       public List<Column> Columns = new List<Column>();
       public int DefaultColumnWidth
       {
           get
           {
               return _DefaultColWid;
           }

           set
           {
               if (value != _DefaultColWid)
               {
                   _DefaultColWid = value;
                   List<Column> col = new List<Column>();
                   foreach (var item in Columns)
                   {
                       item.Width = value;
                       col.Add(item);
                   }
                   Columns = col;
               }
           }
       }
       private int _DefaultColWid = 10;
       public void Test()
       {
           Columns.Add(new Column());
           int x;
           x = Columns[0].Width;
           Console.WriteLine(x);
            DefaultColumnWidth = 121;
           x = Columns[0].Width;
           Console.WriteLine(x);

           DefaultColumnWidth = 45;
           x = Columns[0].Width;
           Console.WriteLine(x);

       }
   }
   public class Column : MyGrid
   {
       private int width = -1;
       public int Width
       {
           get
           {
               if (width >= 0)
                   return width;
               else
                   return base.DefaultColumnWidth;
           }
           set
           {
               width = value;
           }
       }
   }

This is what i have done but this is not the right approach .for implementing you scenario
you need implement a design pattern which is named as OBSERVER here is the link for it.

http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1[^]
 
Share this answer
 
Comments
Grant Mc 26-Sep-13 8:13am    
Your help is really appreciated.

Thank you very much
Er Daljeet Singh 30-Sep-13 5:20am    
Its a pleasure dear for me to help someone.
Firstly inherit Column class from Mygrid class and second make that variable public.then you get the value.

C#
public class MyGrid
  {
      public List<Column> Columns = new List<Column>();
      public int DefaultColumnWidth = 64;
  }

  public class Column:MyGrid
  {
      public Column()
      {
          Width = -1;
      }

      public int Width
      {
          get
          {
              if (Width >= 0)
                  return Width;
              else
                  // return DefaultColumnWidth from parent class
                  return DefaultColumnWidth;
          }
          set;
      }
  }
 
Share this answer
 
Comments
Grant Mc 26-Sep-13 6:40am    
I have tried this, but it does not seem to work.
public class MyGrid
{
public List<column> Columns = new List<column>();
public int DefaultColumnWidth = 64;

public void Test()
{
Columns.Add(new Column());
int x;
x = Columns[0].Width; // returns 64
DefaultColumnWidth = 10;
x = Columns[0].Width; // returns 64
}
}

public class Column : MyGrid
{
private int width = -1;

public int Width
{
get
{
if (width >= 0)
return width;
else
return base.DefaultColumnWidth;
}
set
{
width = value;
}
}
}
Er Daljeet Singh 26-Sep-13 7:06am    
ok i got your question you want to create observer mean if the value from parent class changes it will reflect child class also. i am right

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900