Solved

public override bool IsValid

Posted on 2014-02-14
7
530 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MVC5 Dataformat String for Phone not working 3 26
Achieve json result 2 65
Please explain "Multi-Tenant Services" 5 65
Code works but it's slow 24 44
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

19 Experts available now in Live!

Get 1:1 Help Now