Methods are blocks of code that manipulate the objects that they are created in. In our car example, we can make a ChangeOwner method that will change the name of the owner.
public void ChangeOwner(string newOwner)
{
Owner = newOwner;
}
This method is taking the instance of Car that is called from and changes the Owner to the variable newOwner. Now you may be asking yourself, how does C# know what instance of Car to use? Let me answer that.
On the last page, we created our first object: Car. Here was that code.
Car car = new Car("John Doe", "Honda", "Civic", "White");
In order to change the owner of this car object, we have to use the variable, car.
car.ChangeOwner("Jane Doe");
In order to see our new owner name, lets access the new owner name by using the same syntax.
Console.WriteLine(car.Owner);
Note: we did not need parenthesis because Owner is not a method but a variable inside the Car class.
This should return Jane Doe.
Just like constructors, we can overload methods. It carries the same principles. If we wanted to pass in a Car instance for the ChangeOwner() method instead of a String. We are able to both at the same time.
public void ChangeOwner(Car car)
{
Owner = car.Owner;
}
We would get the same type of error as we did in constructors if we tried to make two ChangeOwner methods with 1 String parameter.
In our bank account class, we added the DepositBalance and WithdrawBalance into our Program.cs. Now that we know how to manipulate objects, we can add it to the Account.cs instead. Here is how we will add it to the Account.cs
public double DepositBalance(double balance, double additionalBalance)
{
Console.WriteLine($"Deposited {additionalBalance}.");
return balance + additionalBalance;
}
public double WithdrawBalance(double balance, double additionalBalance)
{
if (balance - additionalBalance < 0)
{
Console.WriteLine($"Funds insufficient
Max Funds that can be deposited: {balance}");
return balance;
}
Console.WriteLine($"Withdrew {additionalBalance}.");
return balance - additionalBalance;
}
In our Program.cs, we can now edit the if statements to edit the account instead of having 3 variables. Here is the new Main.cs
static void Main(string[] args)
{
string name = "John Doe";
int age = 13;
double balance = 500.00;
Account account = new Account(name, age, balance);
while (true) {
Console.Write($"Hello {name}. You currently have ${balance}. If you want to deposit money, enter deposit.
To withdraw, enter withdraw. If you would like to leave the program, type in quit or q. ");
var input = Console.ReadLine();
if (input.ToLower().Equals("deposit"))
{
Console.Write("How much money would you like to deposit?: ");
var num = Convert.ToInt32(Console.ReadLine());
account.DepositBalance(num);
}
else if (input.ToLower().Equals("withdraw"))
{
Console.Write("How much money would you like to withdraw?: ");
var num = Convert.ToInt32(Console.ReadLine());
account.WithdrawBalance(num);
}
else if (input.ToLower().Equals("quit") || input.ToLower().Equals("q"))
{
Console.WriteLine("Exited the program.");
break;
}
}
}