Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • Administrators
Posted

Does the BankAccount's 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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

ok I'll 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.

 

Here's 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;

}

}

  • Administrators
Posted (edited)

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.

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

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Shouldn't 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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...