C# Problem: Beginning with classes and objects

Updated 7 days ago in [A] C# .Net
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
1 7 days ago

Hey all! I am a returning student and I would be lying if I said that I’m not struggling in my programming classes. I took an intro to program and logic last term based out of C# and this term I’m taking another C# course but it’s more involved. The only way I’m going to learn is by this frustration so I’m hoping for some insight.

Anyways, the application is suppose to give a price per word based on how many words ( and spaces ) are being used for our classified ad. For example, my first object of class ‘ClassifiedAd()’ is named ‘advert’ and has a number of 25 words. I’m trying to use a ‘get’ and ‘set’ method for the math syntax but I think I have it all wrong. Here’s the segment that isn’t working but I feel like should >.<

 // prints the object content to the console
 static void Display(ClassifiedAd advert)
 {
 Console.WriteLine("Your advertisement {0} will be {1} words long and cost a total of {2}.", advert.Category, advert.NumberOfWords, totalCost);
 }
 // create the classified ad class
 class ClassifiedAd
 {
 private int numberOfWords;
 private const double PRICE_PER_WORD = 0.09;
 public double totalCost
 {
 set
 {
 totalCost = numberOfWords * PRICE_PER_WORD;
 }
 get
 {
 return totalCost;
 }
 

What I am getting wrong is the totalCost variable. I’m trying to multiply it and display for each advertisement. 

If it will help I can post the entire thing but only at somebody else’s discretion.
Thank you,
AJ

  • Liked by
Reply

You don’t want the setter in the totalCost property.
So, remove it.
Instead, make the getter return numberOfWords * PRICE_PER_WORD.

  • Liked by
Reply
Cancel