| | |
Product names showing as the class name
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
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?
C# Syntax (Toggle Plain Text)
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(); } }
•
•
Join Date: Oct 2009
Posts: 63
Reputation:
Solved Threads: 16
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
C# Syntax (Toggle Plain Text)
public override string ToString() { return name; }
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
0
#4 Nov 4th, 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
C# Syntax (Toggle Plain Text)
public override string ToString() { return name; }
Of course, thanks that did it!
![]() |
Similar Threads
- How to start a matrix class like this? (C++)
- setup project with different product names (C#)
- I need a class in Java API... (Java)
- JPanel In Standalone Class (Java)
- virtual class with friend functions (C++)
- Fatal Error Class not found, help (PHP)
- Hidden Link - Please help. (PHP)
- After formatting, PC REALLY, REALLY slow (Troubleshooting Dead Machines)
- never taught how to show the VAT of a total. I know that u divide by 1.175 (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: StreamReader and Position
- Next Thread: Help me for this big project digram<=>coding
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database databasesearch datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files focus form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml







