inheritence problem

Tumunzahar

Member
Joined
Jan 1, 2004
Messages
6
Hi

I defined a class BankAccount and then I defined a class CurrentAccount that inherits BankAccount.
In CurrentAccount I put the method public bool CanDebit(double amount)

How can I use this method in the superclass Bankaccount ?


I tried to put the following line in BankAccount :

public extern bool CanDebit(double amount);


That does compile, but I get a run-time error saying

" ... Culture= neutral, PublicKeyToken=null because the method CanDebit has no RVA."

Also the editor tells me to "Consider a DLLImport attribute to specify the external implementation"


What am I doing wrong ?

Thanks.
 
Does the BankAccounts CanDebit have any implementation or is it marked abstract? If it contains implementation then CurrentAccount will automatically gain the CanDebit functionality.
If you wish to replace the BankAccount implementation with a new one then you will need to make the base class version virtual and override it in the sub class.

Is there any code you could post that could give a better idea of what you need to acheive?
 
ok Ill try to be as clear as possible

The implementation of CanDebit is in the class CurrenAccount, which is a subclass of BankAccount. Now I wish to use CanDebit in the superclass Bankaccount.

Heres some code :

BankAccount.cs :

using System;

namespace Reeks6
{
abstract class BankAccount
{
//constructor

public void Debit(double amount)
{
if (CanDebit(amount))
{
balance -= amount;
Console.Write("Debit succeeded ");
}
}
{
}



CurrentAccount.cs :

using System;

namespace Reeks6
{
//constructor (inherits BankAccount)

public bool CanDebit(double amount)
{
if (amount <= balance + overdraftLimit)
{
return true;
}
else
{
return false;
}
}
 
If the function is required by the base class then it needs to be defined in the base class. You could add an abstract function to the base class though and only implement it in the sub class.
C#:
using System;

namespace Reeks6
{
	abstract class BankAccount
	{
		//constructor
		public abstract bool CanDebit(double amount);

		public void Debit(double amount)
		{
			if (CanDebit(amount))
			{
				balance -= amount;
				Console.Write("Debit succeeded ");
			}
		}
	}
}

edit : typo
 
Last edited by a moderator:
Shouldnt I change something in the subclass CurrentAccount aswell ?

I did what you suggested but I get an error saying

CurrentAccount does not implement inherited abstract member BankAccount.CanDebit(double)

and

CurrentAccount.CanDebit(double) hides inherited member BankAccount.CanDebit(double). To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
 
In the current account declare the function as
C#:
public override bool CanDebit(double amount)
{
if (amount <= balance + overdraftLimit)
{
return true;
}
else
{
return false;
}
 
Back
Top