Check input of setter in C# arrays -


i made class "kleurencombinatie" , in class there array , set.

byte[] combinatie; public byte[] combinatie {     { return combinatie; }     set { combinatie = value; }  } 

i can chance values of array doing

kleurencombinatie combo = new kleurencombinatie(); combo.combinatie[0]++; 

but problem creates overflow. idea validate input of setter. can modulo(%). dont know how in setter.

example how wanted be:

byte aantalkleuren; public byte aantalkleuren {     { return aantalkleuren; }     set { aantalkleuren = value % 7; }     //the max value of 6 now. on 6 starts again @ 0 } 

a solution be, making function this. think possible in setter itself.

any ideas how? thanks!

if expose raw array through property that, cannot intercept writes elements in order validate values.

however, instead write own class implements indexer:

public sealed class rangecheckedbytearray {     public rangecheckedbytearray(int size)     {         _data = new byte[size];     }      public byte this[int index] // indexer ensures values checked.     {                 {             return _data[index];         }          set // nobody can set element's value without coming through here.         {             _data[index] = (byte)(value%7);         }     }      private readonly byte[] _data; } 

use in place of byte array, , need:

rangecheckedbytearray combinatie; public rangecheckedbytearray combinatie {     { return combinatie; }     set { combinatie = value; }  } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -