I am making a program for a fruit stand and I like for it to show the names of the products and not the name of the derive class. I think I need a method or rewrite my code somewhere, could someone take a look and give me a hand?

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

In your product class, overload the ToString() function and have it return name. Something (but probably not exactly, because I'm typing this from memory) like

public override string ToString() { return name; }

You should also implement IConvertible and IFormattable

In your product class, overload the ToString() function and have it return name. Something (but probably not exactly, because I'm typing this from memory) like

public override string ToString() { return name; }

Of course, thanks that did it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.