C
C#2y 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
Angius2y 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
moshimoshiOP2y ago
ahhh - is that the only context where args are needed? when passing input into method?
Pobiega
Pobiega2y ago
Thats literally what they do 🙂
moshimoshi
moshimoshiOP2y ago
Got it! 🙂
Pobiega
Pobiega2y 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
moshimoshiOP2y ago
Im thinking yes - since we need to input zodiac info?
Angius
Angius2y ago
You need to input the year
moshimoshi
moshimoshiOP2y 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
Pobiega2y ago
constructors?
Angius
Angius2y ago
Constructor is there to ensure the values are set when creating an instance of this class
Pobiega
Pobiega2y 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
moshimoshiOP2y ago
yeap - thats the one im reeferrng to
Angius
Angius2y ago
What is questionable, though, is the use of properties with backing fields instead of autoproperties
moshimoshi
moshimoshiOP2y ago
what do you mean!
Angius
Angius2y ago
Also, those are fields, not attributes
Angius
Angius2y ago
$structure
MODiX
MODiX2y 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
Angius2y ago
$getsetdevolve
MODiX
MODiX2y 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
Pobiega2y 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
moshimoshiOP2y ago
nope
Pobiega
Pobiega2y ago
okay, then it MUST always have an account number so a constructor is a good way to guarantee that
moshimoshi
moshimoshiOP2y 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
Pobiega2y 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
moshimoshiOP2y ago
why cant i do this
acctNumber = ""; //constructors
acctHolderID = "";
balance = 0;
acctNumber = ""; //constructors
acctHolderID = "";
balance = 0;
Pobiega
Pobiega2y 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
moshimoshiOP2y 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
Pobiega2y 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
moshimoshiOP2y 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
Pobiega2y ago
Not that specific, no.
moshimoshi
moshimoshiOP2y ago
no worries! 😄
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server