question about this code

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?
 
R

Randy A. Ynchausti

John,
public override bool Equals(object obj)
{
if ((obj == null) || (GetType() != obj.GetType()))
return false;
Account acct=(Account)obj;
return acct.balance == balance;
}
}
My question is, why is it necessary to cast the obj parameter to the
Account type?

The reason is because it is passed into the Equals method as a System.Object
type. Since System.Object is the base type for almost every other class in
the system, obj could really be any kind of object. To be able to use the
behavior of an Account on obj (of which type it may ultimately be), you have
to cast it to an Account.

Regards,

Randy
 
G

Guest

Hi John,
you are right that if it got past the if statement then it must be of type
account, but if you don't cast the object to an Account object then the
compiler will complain because you have said that object is of type object,
by passing it as a parameter of type object, therefore the compile would
complain if you tried to say obj.Balance because the definition of object
does not contain a property called balance.

Underneath it is an Account but you have to be explicit with the cast to
let the compiler know what kind of object you think it is.

Mark
 
J

John Salerno

Mark said:
compiler will complain because you have said that object is of type object,

Ah, that makes it clearer! I'm sure that sounds really simply to you
all, but until it was more or less worded that way, I didn't quite get
it. :)
 
E

Ed Courtenay

Mark said:
Hi John,
you are right that if it got past the if statement then it must be of type
account, but if you don't cast the object to an Account object then the
compiler will complain because you have said that object is of type object,
by passing it as a parameter of type object, therefore the compile would
complain if you tried to say obj.Balance because the definition of object
does not contain a property called balance.

Underneath it is an Account but you have to be explicit with the cast to
let the compiler know what kind of object you think it is.

Mark

Of course, you could make life simpler for yourself by using the 'as'
operator:

public override bool Equals(object obj)
{
Account acct = obj as Account;

if (acct == null)
return false;

return acct.balance == balance;
}
}

If obj is not an Account (or a derivation of Account) then acct will be
set to null.
 
J

John Salerno

Ed said:
Of course, you could make life simpler for yourself by using the 'as'
operator:

public override bool Equals(object obj)
{
Account acct = obj as Account;

if (acct == null)
return false;

return acct.balance == balance;
}
}

If obj is not an Account (or a derivation of Account) then acct will be
set to null.

Nice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top