c# - How to extract method from its class in order to reused it in many places and keep encapsulation? -


i'm writing atm software in order train object-oriented programming , have got pin class verifypin method.

    class pin {     private int _pin;      public void changepin (int newpin)     {         if (newpin.tostring().length != 4)         {             throw new argumentexception("pin must 4 digits long");         }          _pin = newpin;     }      public bool verifypin (int pin)     {         return (_pin == pin);     }   } 

i'm trying call method inside other classes in order verify pin stored inside card, account.

so far came idea this, i'm sure there better way

    class card {     private pin _pin;      public card(pin pin)     {         this._pin = pin;         public bool verifypin (int pin)     {         return this._pin.verifypin(pin);     } } 

any sources learn more subject welcomed well.

i pin should encapsulated in account, not card. card associated account. verify pin when given card call following code:

bool verified = mycard.account.verifypin(pin); 

(a static method doesn't work because can't access field _pin).


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -