Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 258225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:09:46+00:00 2026-05-11T22:09:46+00:00

I have a class Employee. I want to be able to Validate() it before

  • 0

I have a class Employee. I want to be able to Validate() it before I save it to make sure all the fields have been populated with valid values.
The user of the class may call Validate() before they call Save() or they may call Save() directly and Save() will then call Validate() and probably throw an Exception if validation fails.

Now, my (main) question is this;
If my Validate() function returns a simple bool then how do I tell the user of the class what is wrong, i.e. “Email not filled in”, “ID not unique” etc. For the purposes of this I just want the error strings to pass to the human user, but the principle is the same if I wanted a list of error codes (except that makes the use of a bitmap more logical).

  • I could use an Out paramater in my Validate function but I understand this is frowned upon.
  • Rather than returning a bool, I could return a string array from my function and just test if it was empty (meaning no errors) – but that seems messy and not right.
  • I could create a Struct just to return from this method, including a bool and a string array with error messages, but just seems clunky.
  • I could return a bitmap of error codes instead of a bool and look it up, but that seems rather excessive.
  • I could create a public property “ValidationErrors” on the object which would hold the errors. However, that would rely on me calling Validate() before reading it or explicitly calling Validate from the Property() which is a bit wasteful.

My specific program is in C# but this looks like a fairly generic “best practice” question and one I am sure I should know the answer to. Any advice gratefully received.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:09:46+00:00Added an answer on May 11, 2026 at 10:09 pm

    I would probably go for the bitmap-option. Simply

    [Flags]
    public enum ValidationError {
        None = 0,
        SomeError = 1,
        OtherError = 2,
        ThirdError = 4
    }
    

    …and in the calling code, simply:

    ValidationError errCode = employee.Validate();
    if(errCode != ValidationError.None) {
        // Do something
    }
    

    Seems nice and compact to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have an abstract class Employee, and 2 derived classes; Staff and
Assume we have class Employee . I want to have a field which references
I have an Employee class ( Id,Name,Age) with few objects. I am showing all
I have class Employee which is something like this. class Emp { int EmpID;
I have a class (called Employee. non static) defined in a class library. I
I have the following requirement I have a Employee class: public class Employee {
I have a Employee class with many attributes. One of the attributes is employeeId
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following: MODEL.PY LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),) class Employee(models.Model): fname =
As a simplified example, I have the following data classes: public class Employee {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.