0

I am curious to know what is the proper way of writing a constructor or rather when do I write it this way or the other. I would also like to know why would you change the name of the field in the construcor, like I did in the first constructor with the field address. Thank you for your help.

For example lets say you have a class Shipment with four fields: String item, Double price, String address, Double weight.

 class Shipment
 {
  private string item;
  private double price;
  private string address;
  private double weight;

   public Shipment(string item, double price, string addr, double weight)
   {
      this.item=item;
      this.price=price;
      address=addr;
      this.weight=weight;
   }

   public Shipment()
   {
   item="Football jersey";
   price=35.99;
   address="8520 Washington Dr.Toledo, OH 43612"
   weight=0.400;
  }

 }
  • 2
    I guess because typing `this.address = address` is too many keystrokes? This is largely a matter of personal style. – Sweeper Jun 07 '18 at 10:17
  • 2
    Name your field with an underscore like `private string _item` (which is very very common) and then you don't need a `this` or abbreviation e.g `_item = item` . the world is saved from another pending programming disaster – TheGeneral Jun 07 '18 at 10:18
  • I would prefer to use this keyword to access member variables. You can check the advantage of it here -- https://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa. I agree with @Sweeper, it's a personal style. – user1672994 Jun 07 '18 at 10:21
  • This is entirely down to personal choice. Some people use `this.foo = foo;`; some people prefer a prefix on fields such as `_`, with `_foo = foo;`. Some people would prefer properties with no explicit fields like `public string Foo {get;}`, with `Foo = foo;` – Marc Gravell Jun 07 '18 at 10:42

1 Answers1

0

I would change the default constructor definition like this,

public Shipment : this ("Football jersey", 35.99, "8520 Washington Dr.Toledo, OH 43612", 0.400 )
{
}

This reuses the parameterized constructor and makes your code a bit more concise.

The rest of your code is ok. The usage of this in the constructor is pretty standard and it prevents you from inventing other names for the constructor arguments (ex - addressParams).

WaughWaugh
  • 1,012
  • 10
  • 15
  • Hi, thanks for the answer, but lets say, I instantiate a new instance of the class Shipment, in my Main method, Shipment newShip = new Shipment("Jersey", 50, "8520 Washington", 0.300); then there is no use it, correct? –  Jun 07 '18 at 11:32
  • Then it would call your parameterized constructor. And you have both options -either to pass in parameters, or if you want to initialize it to default values - pass no parameters at all. – WaughWaugh Jun 07 '18 at 11:34
  • The parametrized is the one that has no fixed values, am I correct? –  Jun 07 '18 at 11:37
  • Yes, this constructor: public Shipment(string item, double price, string addr, double weight) – WaughWaugh Jun 07 '18 at 11:41