0
\$\begingroup\$

I began studying C# 5.0 a few days ago and am trying to avoid duplicated code for validation of input values.

class transcript
{
  //Use lamda expression and Func for validation logic
  Func<byte, byte> validate = (grade) =>
  {
    if (grade > 100)
      throw new ArgumentOutOfRangeException("grade can`t be more than 100");
    else
      return grade;
  };

  public string name { get; set; }

  public byte kor
  {
    get { return kor; }
    set { kor = validate(value); }
  }

  public byte eng
  {
    get { return this.eng; }
    set { eng = validate(value); }
  }
}
\$\endgroup\$
  • \$\begingroup\$ Funcs and delegates are cool, but really give their full power when you want to let a different class specify the behavior of this one. There's no reason not to just declare it as a regular ol' private method. \$\endgroup\$ – RubberDuck May 2 '16 at 11:33
  • \$\begingroup\$ @RubberDuck Thanks for your comment! I`m not native so it is little bit hard to understand could you say more easy? what is ol private method? you mean just declare set property logic each property? \$\endgroup\$ – the1900 May 2 '16 at 12:11
  • 1
    \$\begingroup\$ No, I mean private byte Validate(byte grade) { ... } \$\endgroup\$ – RubberDuck May 2 '16 at 12:39
  • \$\begingroup\$ @RubberDuck Oh I get it. I just try to get use to Func & Action, that`s it \$\endgroup\$ – the1900 May 2 '16 at 14:26
1
\$\begingroup\$

Per the Capitalization Conventions on MSDN, types, methods, and properties should be PascalCasemeaning they start with an uppercase letter.


  Func<byte, byte> validate = (grade) =>
  {
    if (grade > 100)
      throw new ArgumentOutOfRangeException("grade can`t be more than 100");
    else
      return grade;
  };

There is no reason to use a Func here. Just use a normal method, like this

private byte Validate(byte grade)
{
    // the method body can go here
}

  public byte kor
  {
    get { return kor; }
    set { kor = validate(value); }
  }

Have you tested this code? The getter and setter both look like a StackOverflowException (via infinite recursion) to me. You need a backing field, like this

private byte kor;
public byte Kor
{
    get { return kor; }
    set { kor = Validate(value); }
}

It's uncommon to use byte instead of int, even if you know your data will fit in a byte. Only optimize for size if you know that you need to do so.

\$\endgroup\$
  • \$\begingroup\$ thanks. but I have a question why should I use int?. I used byte because it is not gonna be higher than 100 \$\endgroup\$ – the1900 May 5 '16 at 16:40
  • \$\begingroup\$ In .NET, your default choice to hold an integer should be System.Int32 which is normally spelled int in C#. You should only use a different type if you have a reason to (working with a HUGE amount of data, interop with native code, etc...). Working with bytes can be awkward in C# - adding or subtracting bytes returns an int instead of a byte so you need to cast. \$\endgroup\$ – default.kramer May 5 '16 at 17:32
  • \$\begingroup\$ So you mean It is better for performance than even I use Byte instead of Int? Ok I`ll accepted. thanks \$\endgroup\$ – the1900 May 6 '16 at 12:52
0
\$\begingroup\$

Here in this particular example, you can do the same with the help of getter and setter properties. they are capable of doing validations as well. with the help of a back up variable you can achieve the target as like the following:

private byte _Grade;

public byte Grade
{
    get
    {
        return _Grade;
    }
    set
    {
        if (_Grade > 100)
            throw new ArgumentOutOfRangeException("grade can`t be more than 100");
        else
            _Grade = value;
    }
}

So if you are trying to assign a value Greater than 100 it will throw ArgumentOutOfRangeException

\$\endgroup\$
  • \$\begingroup\$ As I already said, I tried to avoid code duplicates and getting effective. \$\endgroup\$ – the1900 May 2 '16 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.