Solved

public override bool IsValid

Posted on 2014-02-14
7
536 Views
Last Modified: 2014-02-20
I have the following class. I was wondering if someone could explain me the class. Maybe a line by line explanation will be good. I'm trying to understand public override bool IsValid(object value)

public class CheckValidYear: ValidationAttribute
{
public CheckValidYear()
{
   ErrorMessage = "The earliest opera is Daphne, 1598, by Corsi, Peri, and
Rinuccini";
}


public override bool IsValid(object value)
{
   int year = (int)value;
   if (year < 1598)
   {
      return false;
   }
   else
   {
      return true;
    }
 }
}
0
Comment
Question by:yguyon28
7 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 39858929
if you are just talking about the

public override

part then that just means that it is a virtual method from the base class ValidationAttribute and instead of using the default method in that class you are handling it in your derived class
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39858930
The public override bool IsValid(object value) part is providing an implementation for a method defined by the base ValidationAttribute class. It takes the value, converts it to an int, and then assesses its value.

If the value is less than 1598, then the method returns false to indicate that validation has failed, otherwise it returns true to indicate the value is valid.
0
 

Author Comment

by:yguyon28
ID: 39858944
Thanks for the info but how is it related to CheckValidYear()
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 22

Expert Comment

by:p_davis
ID: 39858949
it is now a method that can be used by checkvalidyear as modified
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39858952
CheckValidYear() is the constructor for the class. It is called implicitly every time an instance of the class is instantiated. All it does in your case is set the value of the classes ErrorMessage property (which is inherited from the base class).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39859064
For completeness:

The public override bool IsValid(object value) part is providing an implementation for a method defined by either the base ValidationAttribute class or some base class further up the inheritance chain. It is quite possible that IsValid of the ValidationAttribute class is itself overriding a base (virtual) definition.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39859073
Except in this case it isn't. IsValid is defined by the base ValidationAttribute class. But the sentiment is worth bearing in mind for other scenarios.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question