Data Copier

Line: Object Line
Easy Object Orientation Learning Outcome One Learning Outcome Two Walkthrough
Type: Walkthrough
You should have completed: Assignment One This topic leads to: Vector Class

Summary

There are two main goals for this exercise. First is to see the difference between passing variables to methods that accept parameters by VALUE and by REFERENCE. Second is to get you started with Classes and Objects by showing you simple versions of the same class from a functional point of view, written in a separate file within the same C# Project. Main() will demonstrate their usage.

Task

  1. Create a new C# Console project in Visual Studio.
  2. Right click the Project name found in the Solution Explorer on the right hand side of Visual Studio, select 'Add', select 'New item...' and finally select 'Class' from the list. Give it the name 'MyObjectClassV123.cs' and click 'Add'.
  3. Write the code below in the file MyObjectClassV123.cs (double click it in the Solution Explorer, right hand side of Visual Studio).
    code snippet
    code snippet
  4. Write the code below in the file Program.cs (double click it in the Solution Explorer, right hand side of Visual Studio).
    code snippet
    code snippet
  5. Run the project and test it out, try changing the integer values across the board and notice how the program output (Console window) changes.
  6. Now try creating multiple Objects based on MyObjectClassV3, giving them different variable names and then changing the values of the Position property for each of them. Display each of the Position properties' value to the Console and observe the changes. Each Object (variable) created this way is unique and capable of holding completely different data than the other Objects, even if they are spawned from the same Class.

Questions

  1. How would you add another method that accepts two parameters, one by VALUE, the other by REFERENCE, that would allow it to change the variable passed by REFERENCE to the value of the variable passed by VALUE? Test it out in Main().
  2. How would you extend the program with another file containing another class called MyObjectClassV4 (name the file appropriately), containing two private properties with get/set methods (one 'int', another 'string')? Test this new class out in Main() by creating a new object based on it and testing out both properties similarly to the how the code above does.