CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Posts
    1

    getting product names and not the class name

    I am making a program for a fruit stand and I am a little confused what I did wrong. The output shows the class name and not the product name. Can anyone give me a hand?

    Code:
    using System; 
    public class Product { 
       protected string name; 
       protected double cost; 
       protected int inStock; 
       public Product(){ 
          this.name = ""; 
          this.cost = 0; 
          this.inStock = 0; 
       } 
       public Product(string name, double cost, int inStock) 
       { 
          this.name = name; 
          this.cost = cost; 
          this.inStock = inStock; 
       } 
       public int GetInStock() { return this.inStock; } 
       public double Buy(int number) 
       { 
          double totalCost = 0; 
          if (number <= this.inStock){ 
             this.inStock -= number; 
             totalCost = number * this.cost; 
          } 
          return totalCost; 
       } 
    } 
    
    public class Fruit : Product { 
       public Fruit(string name, double cost, int inStock) 
       { 
          this.name = name; 
          this.cost = cost; 
          this.inStock = inStock;    
       } 
    } 
    
    public class Veges : Product{ 
       public Veges(string name, double cost, int inStock) 
       { 
          this.name = name; 
          this.cost = cost; 
          this.inStock = inStock; 
       } 
    } 
    public class TestProduct 
    { 
       public static void Main() 
       { 
          Fruit apple = new Fruit("Apple",2.5,20); 
          Console.WriteLine("{0} in stock {1}", apple, 
             apple.GetInStock()); 
           
          Fruit oranges = new Fruit("Oranges",2.3,50); 
          Console.WriteLine("{0} in stock {1}", oranges, 
             oranges.GetInStock()); 
           
          Veges lettuce = new Veges("Lettuce",3,80); 
          Console.WriteLine("{0} in stock {1}", lettuce, 
             lettuce.GetInStock()); 
           
          Veges cucumber = new Veges("Cucumber",4,5); 
          Console.WriteLine("{0} in stock {1}", cucumber, 
             cucumber.GetInStock()); 
           
            Console.ReadKey(); 
       }       
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: getting product names and not the class name

    The reason is because the first writeline param is the class instance.

    Code:
    Fruit apple = new Fruit("Apple",2.5,20); 
          Console.WriteLine("{0} in stock {1}", apple, 
             apple.GetInStock());
    By default, the class name is displayed. If you want to change this, then create a Name property or override the ToString( ) method.

    Name property example:

    Code:
    public string Name { get { return name; } }
    Using it...
    Code:
    Fruit apple = new Fruit("Apple",2.5,20); 
    
    Console.WriteLine("{0} in stock {1}", apple.Name, 
             apple.GetInStock());
    ToString method override
    Code:
    public override string ToString
    {
      return this.name;
    }
    Using it...
    Code:
    Fruit apple = new Fruit("Apple",2.5,20); 
    
     
    Console.WriteLine("{0} in stock {1}", apple, 
             apple.GetInStock());
    Btw, you might consider making the GetInStock( ) a property rather than a method.

    Something like...
    Code:
    public int InStock { get { return this.inStock; } }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured