Product names showing as the class name

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster

Product names showing as the class name

 
0
  #1
Nov 3rd, 2009
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?

  1. using System;
  2. public class Product {
  3. protected string name;
  4. protected double cost;
  5. protected int inStock;
  6. public Product(){
  7. this.name = "";
  8. this.cost = 0;
  9. this.inStock = 0;
  10. }
  11. public Product(string name, double cost, int inStock)
  12. {
  13. this.name = name;
  14. this.cost = cost;
  15. this.inStock = inStock;
  16. }
  17. public int GetInStock() { return this.inStock; }
  18. public double Buy(int number)
  19. {
  20. double totalCost = 0;
  21. if (number <= this.inStock){
  22. this.inStock -= number;
  23. totalCost = number * this.cost;
  24. }
  25. return totalCost;
  26. }
  27. }
  28.  
  29. public class Fruit : Product {
  30. public Fruit(string name, double cost, int inStock)
  31. {
  32. this.name = name;
  33. this.cost = cost;
  34. this.inStock = inStock;
  35. }
  36. }
  37.  
  38. public class Veges : Product{
  39. public Veges(string name, double cost, int inStock)
  40. {
  41. this.name = name;
  42. this.cost = cost;
  43. this.inStock = inStock;
  44. }
  45. }
  46. public class TestProduct
  47. {
  48. public static void Main()
  49. {
  50. Fruit apple = new Fruit("Apple",2.5,20);
  51. Console.WriteLine("{0} in stock {1}", apple,
  52. apple.GetInStock());
  53.  
  54. Fruit oranges = new Fruit("Oranges",2.3,50);
  55. Console.WriteLine("{0} in stock {1}", oranges,
  56. oranges.GetInStock());
  57.  
  58. Veges lettuce = new Veges("Lettuce",3,80);
  59. Console.WriteLine("{0} in stock {1}", lettuce,
  60. lettuce.GetInStock());
  61.  
  62. Veges cucumber = new Veges("Cucumber",4,5);
  63. Console.WriteLine("{0} in stock {1}", cucumber,
  64. cucumber.GetInStock());
  65.  
  66. Console.ReadKey();
  67. }
  68. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 63
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 17
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
0
  #2
Nov 3rd, 2009
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
  1. public override string ToString() { return name; }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #3
Nov 3rd, 2009
You should also implement IConvertible and IFormattable
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster
 
0
  #4
Nov 4th, 2009
Originally Posted by mikiurban View Post
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
  1. public override string ToString() { return name; }

Of course, thanks that did it!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 198 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC