c# - The use of :base() in a constructor -


this question has answer here:

i trying construct object derives different object, before calling base constructor make few argument validation.

public fuelmotorcycle(string owner) : base(owner) {     argumentvalidation(owner); } 

now understood base constructor called first, there way can call after argumentvalidation method?

now understood base constructor called first, there way can call after argumentvalidation method?

no, @ least not directly.

but can use small workaround have static method takes argument, validates , returns if it's valid or throws exception if it's not:

private static string argumentvalidate(string owner) {     if (/* validation logic owner */) return owner;     else throw new yourinvalidargumentexception(); } 

then have derived class constructor pass arguments through method before giving them base constructor:

public fuelmotorcycle(string owner) : base(argumentvalidate(owner)) { } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -