p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Visual C# 2005 (http://p2p.wrox.com/forumdisplay.php?f=222)
-   -   what is asp.net (http://p2p.wrox.com/showthread.php?t=57002)

Larryz May 3rd, 2007 05:57 PM

Thats an excellent explanation..... thanks.. no one else responds to my questions they must be lame lol..... i mean not they my questions must be....

i have kinda worked out what colllections are, but finding indexers hard..and the concept in general, i get its a collection of objects etc.....


eg public mycollection this[int mycollectindex]
// what is the this refering to...
{
get
{
return (mycollection) List[mycollectindex];
}
// here i guess it returns mycollection with the index value mycollectindex ?

set
{
List[mycollectindex] = value;
// what is this returning
}

in this code....
from the book ...... page 282
Animals is a collection...
Animals animalCollection = new Animals();
animalCollection.Add(new cow("jack"));
animal is an object allowing cow to be derived from it
what confuses me is the call to Add which an Animal object..
i am assuming the obect is first create via the code...new cow("jack") which it will look at the Animal object to see what to do.... and then that object jack is added to the collection... otherwise it wont know what object jack is in order to add it......?

}


dparsons May 3rd, 2007 11:06 PM

In so far as people in here being Lame, that isn't the case. There are alot of skilled contribtors here (Imar, Peter, Joe, Happy, etc) and if I wasn't answering your questions, I am sure one of them would be! ;]

Anyway, about collections.

I don't have the book that you are referencing so what I am going to do is explain this using an array list and the DataTable object.

ArrayList arr = new ArrayList();
arr.Add(new DataTable("foo"));

Now, depending on what I am doing and what the use of this particular collection is I can do something like:

foreach(DataTable t in arr)
{
//do something with each object in the collection
}

Your get statement is returning the object at the given index your set statement doesn't return anything, it sets the specified index with the supplied value.

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========

Larryz May 4th, 2007 06:38 AM

thats great but i dont have a clue of what a DataTable is....

i am , the newbie here...

Anyway i have worked out basicly a collection in terms of the book is a collection of obejects... which different from an array of objects..

eg Animal[] animalArray = new Animal[2];

Its uses and arraylist..
ArrayList AnimalArrayList = new ArrayList();
this ArrayList together with CollectionBase allows collections to be created ....

But i am not sure of all that... a cleaner explanation than the book would, be Very helpfull.....

thanks in advance ..Larry .


dparsons May 4th, 2007 09:07 PM

Hmmm. You have lost me.

An arraylist implements these interfaces: ICollections, IEnumerable, IList, and ICloneable

This is what gives it the ability to create a Collection of objects; for example: when you call Arraylist.Add[value] the Add property is defined in the IList interface and then implemented inside of the System.Collections.ArrayList class to provide this functionality.

I am sorry but i am not sure if i have answered your question. =
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========

Larryz May 6th, 2007 12:59 PM

OK a slight change in topic

Ordinary properties and indexers i am kind of stuck by syytax
and understanding how they work....

An ordinary property, maybe defined as follows.


public int myProperty

get
{
 return myInt;
}
set
{
myInt = value;
}

In the above example, we may call it
via
myobject.myProperty = 6;

hence using the set branch to set the value..

we use the name myProperty to set that property and we can see
from the property signature that it will set an integer value.

public int myProperty

i hope i am making sense ....

But take this example...

public Animal this[int animalIndex]
// This is a special property called and indexer..
// it is used in object collections
get
{
 return (Animal) List[animalIndex];
}
set
{
  List[animalIndex] = value;
}

The confusing bit for me..... is

A) The signature of the property

Ordinary properties and indexers i am kind of stuck by syytax
and understanding how they work....

An ordinary property, maybe defined as follows.



public int myProperty
//its public it sets an integer type the name is myProperty.
// we explictly call it.. to use it.

get
{
 return myInt;
}
set
{
myInt = value;
}

In the above example, we may call it
via
myobject.myProperty = 6;

hence using the set branch to set the value..

we use the name myProperty to set that property and we can see
from the property signature that it will set an integer value.

public int myProperty

i hope i am making sense ....

But take this example...

public Animal this[int animalIndex]

Animal is s class so its not the name
and its not called directly as in the above example
how does the compiler know that
when for example
            animalsCollection[2] = new chicken("franky")
to use this indexer...
In the normal property we call it via its name....

and point B
I dont get the property signature of the indexer

public Animal this[int animalIndex]
What are all these bits ...What is Animal what is this[in animalIndex]

i cant break down the signature as i did for ordinary properties.

There isnt a name for the property as such in the signature.

All i can think of is the object name is the name and it takes and index.
and it sets an object type of Animal.



Larryz May 7th, 2007 08:09 AM


Posted - 05/06/2007 : 11:42:54 PM Show Profile Email Poster Edit Topic Send Larryz an AOL message Reply with Quote Delete Topic
public void CopyTo(Cards targetCards)
      {
         for (int index = 0; index < this.Count; index++)
         {
            targetCards[index] = this[index];
         }
      }

The code above is called via objectName.CopyTo(cards)
cards is an collection....

what is the this refering to... is it... objectName.count, where objecctName is a collection.

thanks Larry..



All times are GMT -4. The time now is 06:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.