C
C#17mo ago
moshimoshi

❔ use of args in method

can someone explain the specific context in which we declare args in the methods? e.g. for this method - no args are declared whereas for another mtd where new variables are introduced, args are declared. Here's code:
public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (isWithdrawOk)
{
another.Deposit(amount);
return true;
}
else return false;
}
public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (isWithdrawOk)
{
another.Deposit(amount);
return true;
}
else return false;
}
public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
32 Replies
Angius
Angius17mo ago
You want arguments when you need to pass something into the method If the method doesn't take any input, you don't need arguments
moshimoshi
moshimoshi17mo ago
ahhh - is that the only context where args are needed? when passing input into method?
Pobiega
Pobiega17mo ago
Thats literally what they do 🙂
moshimoshi
moshimoshi17mo ago
Got it! 🙂
Pobiega
Pobiega17mo ago
Example: I want to write a method that gets me the chinese zodiac sign for a given year Would this method need an argument?
moshimoshi
moshimoshi17mo ago
Im thinking yes - since we need to input zodiac info?
Angius
Angius17mo ago
You need to input the year
moshimoshi
moshimoshi17mo ago
ahhh right I also have another question, I dont really understand the use of the constructors here (it seems like unnecessary double work) - is it 'right' to completely omit it?
public class Account
{
private string acctNumber; //attributes
private string acctHolderID;
private double balance;

public Account(string number, string holder, double bal)
{
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
}

public string AccountNumber //properties
{
get
{
return acctNumber;
}
}

public string AccountHolderID
{
get
{
return acctHolderID;
} set
{
acctHolderID = value;
}
}

public double Balance
{
get
{
return balance;
} set
{
balance = value;
}
}

public void Deposit(double amount)
{
balance += amount;
}

public virtual bool Withdraw(double amount)
{
balance -= amount;
return true;
}

public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (isWithdrawOk)
{
another.Deposit(amount);
return true;
}
else return false;
}

public virtual double CalculateInterest()
{
return 0;
}

public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
}
public class Account
{
private string acctNumber; //attributes
private string acctHolderID;
private double balance;

public Account(string number, string holder, double bal)
{
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
}

public string AccountNumber //properties
{
get
{
return acctNumber;
}
}

public string AccountHolderID
{
get
{
return acctHolderID;
} set
{
acctHolderID = value;
}
}

public double Balance
{
get
{
return balance;
} set
{
balance = value;
}
}

public void Deposit(double amount)
{
balance += amount;
}

public virtual bool Withdraw(double amount)
{
balance -= amount;
return true;
}

public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (isWithdrawOk)
{
another.Deposit(amount);
return true;
}
else return false;
}

public virtual double CalculateInterest()
{
return 0;
}

public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
}
Pobiega
Pobiega17mo ago
constructors?
Angius
Angius17mo ago
Constructor is there to ensure the values are set when creating an instance of this class
Pobiega
Pobiega17mo ago
public Account(string number, string holder, double bal)
{
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
}
public Account(string number, string holder, double bal)
{
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
}
is your only constructor here
moshimoshi
moshimoshi17mo ago
yeap - thats the one im reeferrng to
Angius
Angius17mo ago
What is questionable, though, is the use of properties with backing fields instead of autoproperties
moshimoshi
moshimoshi17mo ago
what do you mean!
Angius
Angius17mo ago
Also, those are fields, not attributes
Angius
Angius17mo ago
$structure
MODiX
MODiX17mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
For C# versions older than 10, see $StructureOld
Angius
Angius17mo ago
$getsetdevolve
MODiX
MODiX17mo ago
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
Pobiega
Pobiega17mo ago
But yeah, a contructor (commonly called a ctor) has one responsibility: Make sure your object has a "valid state" when it is created. can an account exist without an account number?
moshimoshi
moshimoshi17mo ago
nope
Pobiega
Pobiega17mo ago
okay, then it MUST always have an account number so a constructor is a good way to guarantee that
moshimoshi
moshimoshi17mo ago
i see, and the 'number', 'holder' and 'bal' fields are just used as placeholders in this instance?
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
Pobiega
Pobiega17mo ago
they are method arguments 🙂 they are values passed in to the constructor if your ctor doesnt do anything with them, they do nothing.
moshimoshi
moshimoshi17mo ago
why cant i do this
acctNumber = ""; //constructors
acctHolderID = "";
balance = 0;
acctNumber = ""; //constructors
acctHolderID = "";
balance = 0;
Pobiega
Pobiega17mo ago
you can, but now you have an account with no account number or well, a blank strink is the account number. not a great number.
moshimoshi
moshimoshi17mo ago
got it! Thanks so much!!! another question, if i were to use to auto 'get' 'set' property, how will the system know which variable im pointing to without explicitly calling it out?
public class Account
{
private string acctNumber; //attributes
private string acctHolderID;
private double balance;

public Account(string number, string holder, double bal)
{
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
}

public string AccountNumber { get; }
public string AccountHolderID { get; set; }
public double Balance { get; set; }

public void Deposit(double amount)
{
balance += amount;
}

public virtual bool Withdraw(double amount)
{
balance -= amount;
return true;
}

public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (isWithdrawOk)
{
another.Deposit(amount);
return true;
}
else return false;
}

public virtual double CalculateInterest()
{
return 0;
}

public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
}
public class Account
{
private string acctNumber; //attributes
private string acctHolderID;
private double balance;

public Account(string number, string holder, double bal)
{
acctNumber = number; //constructors
acctHolderID = holder;
balance = bal;
}

public string AccountNumber { get; }
public string AccountHolderID { get; set; }
public double Balance { get; set; }

public void Deposit(double amount)
{
balance += amount;
}

public virtual bool Withdraw(double amount)
{
balance -= amount;
return true;
}

public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (isWithdrawOk)
{
another.Deposit(amount);
return true;
}
else return false;
}

public virtual double CalculateInterest()
{
return 0;
}

public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
}
Pobiega
Pobiega17mo ago
remove your attributes they are actually fields "auto-properties" dont need explicit fields, they make their own
public class Account
{
public Account(string accountNumber, string accountHolder, double balance)
{
AccountNumber = accountNumber;
AccountHolderId = accountHolder;
Balance = balance;
}

public string AccountNumber { get; }

// public set? Should this really be changeable from outside the account? `private set;` feels better
public string AccountHolderId { get; set; }

public double Balance { get; private set; }

public void Deposit(double amount)
{
Balance += amount;
}

public virtual bool Withdraw(double amount)
{
Balance -= amount;
return true;
}

public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (!isWithdrawOk) return false;

another.Deposit(amount);
return true;
}

public virtual double CalculateInterest()
{
return 0;
}

public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
}
public class Account
{
public Account(string accountNumber, string accountHolder, double balance)
{
AccountNumber = accountNumber;
AccountHolderId = accountHolder;
Balance = balance;
}

public string AccountNumber { get; }

// public set? Should this really be changeable from outside the account? `private set;` feels better
public string AccountHolderId { get; set; }

public double Balance { get; private set; }

public void Deposit(double amount)
{
Balance += amount;
}

public virtual bool Withdraw(double amount)
{
Balance -= amount;
return true;
}

public bool TransferTo(double amount, Account another)
{
bool isWithdrawOk = Withdraw(amount);

if (!isWithdrawOk) return false;

another.Deposit(amount);
return true;
}

public virtual double CalculateInterest()
{
return 0;
}

public void CreditInterest()
{
var interest = CalculateInterest();
Deposit(interest);
}
}
something like this 🙂
moshimoshi
moshimoshi17mo ago
Got it - this makes perfect sense! Alsoo, i wanted to look for practice questions related to object oriented programming - specifically around inheritence and polymorphism - are there any repositiories you recommend? 🙂
Pobiega
Pobiega17mo ago
Not that specific, no.
moshimoshi
moshimoshi17mo ago
no worries! 😄
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.