Solved

public override bool IsValid

Posted on 2014-02-14
7
527 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now