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

  • Home
  • SEARCH
  • 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 6623771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:34:18+00:00 2026-05-25T21:34:18+00:00

This is my method: /// <summary> /// Uses Dictionary(Key,Value) where key is the property

  • 0

This is my method:

/// <summary>
/// Uses Dictionary(Key,Value) where key is the property and value is the field name.
/// Matches the dictionary of mandatory fields with object properties
/// and checks whether the current object has values in it or
/// not.
/// </summary>
/// <param name="mandatoryFields">List of string - properties</param>
/// <param name="o">object of the current class</    
/// <param name="message">holds the message for end user to display</param>
/// <returns>The name of the property</returns>   
public static bool CheckMandatoryFields(Dictionary<string,string > mandatoryFields, object o,out StringBuilder  message)
{
    message = new StringBuilder();
    if(mandatoryFields !=null && mandatoryFields.Count>0)
    {
        var sourceType = o.GetType();
        var properties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Static);
        for (var i = 0; i < properties.Length; i++)
        {
            if (mandatoryFields.Keys.Contains(properties[i].Name))
            {
                if (string.IsNullOrEmpty( properties[i].GetValue(o, null).ToString()))
                {
                    message.AppendLine(string.Format("{0} name is blank.", mandatoryFields.Values));
                }
            }
        }
        if(message.ToString().Trim().Length>0)
        {
            return false;
        }
    }
    return true;
}

In this I have params Dictionary which will hold the property name of the class and its corresponding fieldname from the UI(manually fed by developer in the business layer or UI).
So what I want is that when the property is on the way to validate, if the property is found null or blank, then its corresponding fieldname, which is actually the value of the dictionary will get added to the stringbuilder message in the method above.

I hope i am clear.

  • 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-25T21:34:18+00:00Added an answer on May 25, 2026 at 9:34 pm

    Do the loop the other way:

    public static bool CheckMandatoryFields(Dictionary<string,string > mandatoryFields, object o,out StringBuilder  message)
    {
        message = new StringBuilder();
        if(mandatoryFields == null || mandatoryFields.Count == 0)
        {
            return true;
        }
    
        var sourceType = o.GetType();
        foreach (var mandatoryField in mandatoryFields) {
            var property = sourceType.GetProperty(mandatoryField.Key, BindingFlags.Public | BindingFlags.Static);
            if (property == null) {
                continue;
            }
    
            if (string.IsNullOrEmpty(property.GetValue(o, null).ToString()))
            {
                message.AppendLine(string.Format("{0} name is blank.", mandatoryField.Value));
            }
        }
    
        return message.ToString().Trim().Length == 0;
    }
    

    This way you iterate over the properties you want to check, so you always have a handle on the “current” property and know the corresponding key and value from the dictionary.

    The snippet

    if (property == null) {
        continue;
    }
    

    causes the function to treat properties that exist as names in the dictionary but not as actual properties on the type to be treated as valid, to mirror what your original code does.

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

Sidebar

Related Questions

/// <summary> /// This method does something... /// </summary> public void DoSomething() { //
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am
Please check the following: /// <summary> /// This method returns a custom column control
I wrote an add-in for Enterprise Architect. It's a COM Object. This add-in uses
I've just finished making this recursive method: /// <summary> /// Recursively process a given
Let's consider the following source code. /// <summary> /// This is a method I
I recently found out about C# extension methods and wrote this one: /// <summary>
This method works as expected - it creates a JTree with a root node
With this method declaration (no overloads): void Method(double d) { // do something with

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.