ObjectsAsParameters.md - brainchildservices/curriculum GitHub Wiki

Slide 1

Using Objects as Parameters in C#

Q1: Write a class Employee that has a method that takes employee details like, first name, last name, middle name, age, address, weight and height as parameters and makes a meaningful sentence using the parameters which gets printed. Use the method to print the details of 4 employees in the Main method.
https://dotnetfiddle.net/8fYYjb Look at the length of the parameter list of method Print which affects the readability of the program.

Q2: There was a requirement that asked to include the salary to the details of the Employee.
No of changes required for this are:

  1. Add an additional parameter to the Print method in class Employee which caused all the previous method calls in the Main method to error out.
  2. Update the Console.WriteLine in the Print method.
  3. Update every individual method call in Main Method.

Slide 2

Another approach of doing this is to create a class with all the parameters as fields or properties. So we define a class with properties for first name, last name, middle name, age, address, weight and height. And then instead of passing each detail separately to the method Print, we pass an object of the class Person. The object of Person has access to the all public members. This makes the code more readable.
https://dotnetfiddle.net/xTqneJ

Now if we are asked to enter the salary details:

  1. We add a property for Salary in the Person class. Our program still works, unlike the previous case. So one of the important things to be taken care of while programming is to make sure that extending the program doesn't cause our previous version to break.
  2. Update the Console.WriteLine in the Print method.
  3. Pass the value of salary to all the Person object.
    https://dotnetfiddle.net/qSf1CU

Slide 3

Method returning an object

Similar to the way we return int, string and array as return from methods we can return objects as well. Instead of int,string etc in the return type, the name of the class is provided. And the return will be an object of the class.

  AccessModifier ClassName MethodName(parameters)  
  {  
  Method body;  
  return objectofClassName;  
  }

Eg: We first create a class Rectange which has two properties leght and breadth. There is a method Area which calculates and returns an integer value.
https://dotnetfiddle.net/Q3tEEW

Now we get a new requirement where we are asked to enlarge the rectangle by a given factor.
So we create a new class ShapeChanger. Inside it we create a class Enlarge.
The Enlarge method takes rectangle as one parameter and factor by which the shape is to be enlarged as the other parameter.
Inside the method we increase the size size and length by factor times the original size of the rectangle.
And then return the rectangle that is now bigger than the original. In the main method we call the Enlarge method and pass the previously created rectangle object.
And after that we try to print the Area of the rectangle and we could see that the area has increased.
https://dotnetfiddle.net/S1ixy0

Slide 4

Exercise:

  • Q: Create a class Time. It has two fields hour and minute.
    Create a method GetTime that when called prompts the user to enter the hour and minute and the values read and saved to the fields hour and minute.
    Create a method Display that prints the time using the hour and minute fields.
    Call the methods in Main method. https://dotnetfiddle.net/XaIaue
    You are asked to add an additional functionality to your program. You need to take two Time parameters and print the sum of the two Times.
    For that create a new class TimeChanger. In that create a method AddTime. The AddTime method takes two parameters other are Time class objects.
    Create an object of class Time inside the method. Inside the method add the individual hours of both objects and save to hour field of the created Time object, add the individual minutes of both objects and save to hour filed of the created Time object. And return the Time object.
    https://dotnetfiddle.net/28pcdh

References:
https://www.codeguru.com/columns/dotnet/using-objects-as-parameters-in-c.htm
https://www.geeksforgeeks.org/c-sharp-method-returning-an-object/