2 Replies - 115 Views - Last Post: 05 February 2012 - 07:51 PM Rate Topic: -----

Topic Sponsor:

#1 jim01  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 26-January 12

Calling constructors with parameters when parameters are user inputted

Posted 05 February 2012 - 05:20 PM

My book gives several examples of creating constructors and instantiating objects by calling the constructors. However, in all the examples, the values for the variables are put directly into the constructor. Here is one of the examples:


public CarpetCalculator(double amountNeeded, double price)
{
    noOfSqYards = amountNeeded;
    pricePerSqYard = price;
}

CarpetCalculator plush = new CarpetCalculator(37.90, 17.95);




Ok, that's fine, I understand that. However, what if the variables "amountNeeded" and "price" are derived from user inputs? Then how would you call the constructor?

I tried this but received an error message:


CarpetCalculator plush = new CarpetCalculator(amountNeeded, price); 




I have no idea what else to try.

Is This A Good Question/Topic? 0
  • +

Replies To: Calling constructors with parameters when parameters are user inputted

#2 Sergio Tapia  Icon User is offline

  • Is that a raincoat?
  • member icon

Reputation: 1012
  • View blog
  • Posts: 3,816
  • Joined: 27-January 10

Re: Calling constructors with parameters when parameters are user inputted

Posted 05 February 2012 - 06:29 PM

I'm fairly certain that you tried that without capturing user input into those variables, correct?

Try to set a value to those variables before you use them. Also, copy paste the error you got, we aren't wizards. ;)
Was This Post Helpful? 0
  • +
  • -

#3 jim01  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 26-January 12

Re: Calling constructors with parameters when parameters are user inputted

Posted 05 February 2012 - 07:51 PM

Thank you for the response but I figured it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1