Tumunzahar Posted January 3, 2004 Posted January 3, 2004 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. Quote
Administrators PlausiblyDamp Posted January 3, 2004 Administrators Posted January 3, 2004 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Tumunzahar Posted January 4, 2004 Author Posted January 4, 2004 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; } } Quote
Administrators PlausiblyDamp Posted January 4, 2004 Administrators Posted January 4, 2004 (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 January 4, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Tumunzahar Posted January 4, 2004 Author Posted January 4, 2004 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. Quote
Administrators PlausiblyDamp Posted January 4, 2004 Administrators Posted January 4, 2004 In the current account declare the function as public override bool CanDebit(double amount) { if (amount <= balance + overdraftLimit) { return true; } else { return false; } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.