mardi 5 janvier 2016

Conditions on a contructors arguments

Is it possible to check the arguments of a constructor for certain constraints and if they're not met the object is not created and return a value to tell it failed to be created .

for instance .

Class Device
{
   string id;
   Device(string ID)
   {
          If (ID.Length != 7)
          {
              //Do not create Object
          }
    id == ID;
   }

}

Here I only want 7 char long id String , not less not more ! if its not 7 char I don't want the Object created is it possible to do this ?

I couldn't think of any solution to this other than external function check which is something I want to stay away from !

Aucun commentaire:

Enregistrer un commentaire