Solved

public override bool IsValid

Posted on 2014-02-14
7
543 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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