Monday, March 26, 2012

Working with classes

Hi all!

.NET Framework Library: I can't understand how working with this hierarchy model(please be patient - newbie):


// Create a new row object.
TableRow rowNew = new TableRow();
// Add row to the table
tblViewState.Rows.Add(rowNew);

Rows is a property fromTable class fromSystem. Web.UI.WebControls namespace.
System. Web.UI.WebControls.Table.Rows

Add is a method fromTableRowCollection fromSystem. Web.UI.WebControls namespace.
System. Web.UI.WebControls.TableRowCollection.Add

In code above are we adding a method from a class into a property from another class?
class inside class? How do we know when is possible to make this "joining"?
My question is about the way this line of code was build - could somebody explain it?

I'll appreciate any help
thanks
welI'll start with an anology ...

Let's say you know that you may be asked to carry 20 apples. Now, there is no way you can carry 20 apples in your hands. So, you'd avail yourself of a basket.

So, we have you (one object), holding a basket (another object). Someone now wants to hand you an apple (a third object).

They can't put the apple into your hands, since they are not available (they're holding the basket). So, they put it into your basket.

Using pseudo-code, we could write this as:
ThisApple = New Apple()
Person.Basket.Add( ThisApple )

In english, this says that we are creating a new Apple (object), then adding the Apple object to the Basket (another object), which in turn belongs to you, the person (another object).

So, extending this analogy to the code you showed.

Instead of apples, we are looking at table rows.

The Table is like you ... the ultimate "holder" of objects.
But, like you, it doesn't directly hold the rows ... instead, it uses a basket.

The basket here is its Rows property.
You've already shown that the Table.Rows is defined as a TableRowCollection. It is just a collection of Rows, in the same way the basket was a collection of apples.

So, to give a new Row (object) to our Table (object), we in fact put it into the Rows collection (object).

The code would now be:
ThisRow = New TableRow()
Table.Rows.Add( ThisRow )

In english, this says that we create a new TableRow object, then add that to the Rows object (which is defined as a TableRowCollection), which in turn belongs to the Table object.

Personally, I find it much easier to think of "objects" rather than "classes" (and they're tightly related) ... since objects can be visualised in the way that (I hope) I've done here ...

Does that help?
Firstly I'm so pleased with your comments, thank you very much and my sincere compliments for your reply!!!
So clearly, so didactic!!!

You are really right when call all this by objects - this language provides a full set of object oriented programming concepts - I ever heard developers working with objects - by creating code that describes something from the real world - like your example.

I come from classical asp thats not properly a programming language and I think the hard day by day programming will provide necessary knowlegde to understand this new mentality: when creatingobjects, their members and how to join them.

Intelligent and helpful answers like yours and this great community certainly will help our growth.

Best regards
wel

0 comments:

Post a Comment