Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Beginning Visual C# 2005
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 3rd, 2007, 10:02 AM
Authorized User
 
Join Date: Apr 2007
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Larryz
Default Generics lond discussion simple code

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();
        }
    }
}








Similar Threads
Thread Thread Starter Forum Replies Last Post
Very Simple code Tal1481 Beginning VB 6 9 January 29th, 2007 11:12 AM
Very Simple code Tal1481 Visual Basic 2005 Basics 1 January 25th, 2007 07:15 AM
Simple HTML code... rupen HTML Code Clinic 1 October 31st, 2006 06:36 PM
Generics Related error in my code, I think??? rodmcleay Generics 1 August 3rd, 2006 12:02 AM
Discussion: Code-Behind vs. Inline in ASP.Net planoie ASP.NET 1.x and 2.0 Application Design 3 September 20th, 2004 02:25 PM





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