Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Beginning Visual C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2005
This is the forum to discuss the Wrox book Beginning Visual C# 2005 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780764578472
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 13th, 2007, 06:51 AM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Larryz
Default Chapter 12 any one read it..

I wrote the following simple class below to clear up some points.

The class below that is from a book, and I am using it to try and make the code clear to me…

Can people please comment on what I am saying, I am sure a lot of it wont be accurate.

My comments:

The class below has a defualt and non default construct used to create instances of the class student.. Without these an object of this type cant be contructed or instaniated.

When this line is called

blank = new student();

a blank template is created for the student class. Putting a “” into the name veriable and zeror into the id…

when either property such as myStudent.Name it uses the get to return the value…

Now moving onto the Generic class Farm<T>

Where is the defualt construct ?

private List<T> animals = new List<T>();


this apppears close to it and it appear to me that the property
public List<T> Animals
        {
            get
            {
                return animals;
            }
        }
Animals is being used like a handle to set up the animals collection
Eg..

farm.Animals.Add(new Cow("Jack"));
Animals uses the get to return animals the actual collection for storing the collection of animals
If we substitute animals for Animals we are adding via animals.Add to this collection ?

Initialy to me, it seems the get should be be a set as this seems to be setting values ?

I see this simulary to mystudent.Name which is a get to access name, Via Name.


These lines are confusing

public IEnumerator<T> GetEnumerator()
        {
            return animals.GetEnumerator();
        }

I can see it’s a method, and what it returns, there is no yield and its not implemented as in the usual way…

Also the line return animals.GetEnumerator(); The book says you simply return the enumerator returned by Animals…. ?

But Farm<T> : IEnumerable<T> inherits from this inteface not Animals ?

animals is just a type, that contains the collection of animals, I can see you are trying to iterate through it in the foreach loop, but where is it implemented..

should it be (obviously not it wont work) but Farm<T>.Animals.GetEnumerator()

what is this a method a it looks odd the way its defined plus where is it implemented..

thanks Laurence

Also I am aware ArrayList() implements GetEnumerator() as it uses a foreach command.
IEnumerator IEnumerable.GetEnumerator()
        {
            return animals.GetEnumerator();
        }


using System;
using System.Collections.Generic;
using System.Text;

namespace stud
{
    public class student
    {
        private string name;
        private int Id;
        public student()
        {
            // default contructor.
        }
        public student(string newname, int newId)
        {
            name = newname;
            Id = newId;
        }

        public string Name
        {
            get
          {
              return name;
          }
          set
            {
                name = value;
            }

        }
        public int ID
        {
            get
            {
                return Id;
            }
            set
            {
               Id = value;
            }

        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace stud
{
    class Program
    {
        static void Main(string[] args)
        {
            student blank = new student();
            student myStudent = new student("Frank", 1);
            Console.WriteLine("{0} is students name {1} is students ID", myStudent.Name, myStudent.ID);
            Console.WriteLine(myStudent.ToString());
            Console.WriteLine("{0} is students name {1} is students ID", blank.Name, blank.ID);
            Console.WriteLine(blank.ToString());
            Console.ReadKey();
        }
    }
}


Generic Class is confusing



using System;
using System.Collections.Generic;
using System.Text;

namespace ch12ex02
{
  public abstract class Animal
    {
      protected string name;
      public String Name
      {
          get
          {
              return name;
          }
          set
          {
              name = value;
          }
      }
      public Animal()
      {
          // default constructor
          name = "The animal with no name ";
      }
      public Animal(string newName)
      {
          // non defualt contructor.
          name = newName;
      }
      public void Feed()
      {
          Console.WriteLine("{0} has been fed.", name);
      }
      public abstract void MakeNoise();
    }
}

using System;
using System.Collections.Generic;
using System.Text;


namespace ch12ex02
{
    public class chicken : Animal
    {
        public void LayEgg()
        {
            Console.WriteLine("{0} has laid an egg", name);
        }
        public chicken(string newName): base(newName)
        {
        }
        public override void MakeNoise()
        {
            Console.WriteLine("{0} says Cluck", name);
        }
    }


using System;
using System.Collections.Generic;
using System.Text;


namespace ch12ex02
{
   public class Cow: Animal
    {
       public void Milk()
       {
           Console.WriteLine("{0} has been milked.", name);
       }
       public Cow ( string newName ): base(newName)
       {
         //This willl call the non defualt constructor of the base animal class.
       }
       public override void MakeNoise()
       {
           Console.WriteLine("{0} says moo!", name);
       }
    }
}
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace ch12ex02
{
    public class Farm<T> : IEnumerable<T>
        where T : Animal
    {
        private List<T> animals = new List<T>();
        public List<T> Animals
        {
            get
            {
                return animals;
            }
        }
        public IEnumerator<T> GetEnumerator()
        {
            return animals.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return animals.GetEnumerator();
        }
        public void MakeNoises()
        {
            foreach (T animal in animals)
            {
                animal.MakeNoise();
            }
        }
        public void FeedTheAnimals()
        {
            foreach(T animal in animals)
            {
                animal.Feed();
            }
        }
        public Farm<Cow> GetCows()
        {
            Farm<Cow> cowFarm = new Farm<Cow>() ;
            foreach (T animal in animals)
            {
                if (animal is Cow)
                {
                    cowFarm.Animals.Add(animal as Cow);
                }
            }
            return cowFarm;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace ch12ex02
{
   public class SuperCow: Cow
    {
       public void fly()
       {
           Console.WriteLine("{0} is flying", name);
       }
       public SuperCow(string newName)
           : base(newName)
       {
       }
       public override void MakeNoise()
       {
           Console.WriteLine("{0} says here i come to save the day !", name);
       }
    }

}

using System;
using System.Collections.Generic;
using System.Text;

namespace ch12ex02
{
    class Program
    {
        static void Main(string[] args)
        {
            Farm<Animal> farm = new Farm<Animal>();
            farm.Animals.Add(new Cow("Jack"));
            farm.Animals.Add(new chicken("vera"));
            farm.Animals.Add(new chicken("Sally Ally"));
            farm.Animals.Add(new SuperCow ("Kevin"));
            farm.MakeNoises();
            Farm<Cow> dairyFarm = farm.GetCows();
            dairyFarm.FeedTheAnimals();
            foreach (Cow cow in dairyFarm)
            {
                if (cow is SuperCow)
                {
                    (cow as SuperCow).fly();
                }
            }
            Console.ReadKey();
        }
    }
}



Reply With Quote
  #2 (permalink)  
Old August 2nd, 2007, 09:37 AM
Registered User
 
Join Date: Aug 2007
Location: Doncaster, n/a, United Kingdom.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm no expert but here goes:-

If we substitute animals for Animals we are adding via animals.Add to this collection ?
<You would not be able to do this anywhere other than in the FARM<T> class as animals is a private List. But if done there then you would use the build in .Add method of List Generic Class>

public IEnumerator<T> GetEnumerator()
        {
            return animals.GetEnumerator();
        }
<Because you have implemented the IEnumerable<T> interface you have to define the GetEnumerator() method in the generic class, this is done here>

lso the line return animals.GetEnumerator(); The book says you simply return the enumerator returned by Animals…. ?

But Farm<T> : IEnumerable<T> inherits from this inteface not Animals ?

animals is just a type, that contains the collection of animals, I can see you are trying to iterate through it in the foreach loop, but where is it implemented..

should it be (obviously not it wont work) but Farm<T>.Animals.GetEnumerator()

what is this a method a it looks odd the way its defined plus where is it implemented..
<FARM<T> implementers the interface IEnumerable<T> but it is restricted by the where T : Animal part of the declaration - this is saying that T must be of type Animal or a derived class then at runtime the this generic class will be of type Animal is declared in this like:-

Farm<Animal> farm = new Farm<Animal>(): in program.cs

As for the iteration the code has been impemented that when you try to iterate the generic class collection via GetEnumerator() you will actual iterate over the members of the animal list instead
>

<Final note as to why the code does not run there is a missing using statement that define the system.collections name space which is needed as the code calls on a method of that class. IEnumerator

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM
Errors on Chapter 12 example(12.8) sonnie ASP.NET 2.0 Professional 2 June 7th, 2006 10:55 AM
Chapter 12... rrlevron BOOK: Beginning ASP 3.0 2 January 16th, 2006 01:09 AM
Chapter 12 Koga VB.NET 2002/2003 Basics 0 November 2nd, 2004 11:43 PM



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


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