J
John Salerno
Here's the code:
// created on 12/21/2002 at 1:42 PM
using System;
public class Account
{
private double balance;
public Account(double startingBalance)
{
balance=startingBalance;
}
public override bool Equals(object obj)
{
if ((obj == null) || (GetType() != obj.GetType()))
return false;
Account acct=(Account)obj;
return acct.balance == balance;
}
}
class AtTheBank
{
[STAThread]
static void Main(string[] args)
{
Account myAccount = new Account(500);
Account mySavings = new Account(500);
Console.WriteLine(myAccount.Equals(mySavings));
}
}
My question is, why is it necessary to cast the obj parameter to the
Account type? The book says because the default object doesn't have a
balance field. I partially understand this, but if obj already passed
the if statement (meaning it is an Account object), wouldn't it have the
balance field?
// created on 12/21/2002 at 1:42 PM
using System;
public class Account
{
private double balance;
public Account(double startingBalance)
{
balance=startingBalance;
}
public override bool Equals(object obj)
{
if ((obj == null) || (GetType() != obj.GetType()))
return false;
Account acct=(Account)obj;
return acct.balance == balance;
}
}
class AtTheBank
{
[STAThread]
static void Main(string[] args)
{
Account myAccount = new Account(500);
Account mySavings = new Account(500);
Console.WriteLine(myAccount.Equals(mySavings));
}
}
My question is, why is it necessary to cast the obj parameter to the
Account type? The book says because the default object doesn't have a
balance field. I partially understand this, but if obj already passed
the if statement (meaning it is an Account object), wouldn't it have the
balance field?