PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How to check an empty object?

 October 28, 2022     c#, is-empty, object, reflection     No comments   

Issue

I have a number of objects. Some of them have string properties and some of them have bool properties. I'm using them in multi-step form. so each step is bound to each object.

Now, if the user fills the first two sections and saves the data as a user wants to fill rest of the data later. At this point, I need to identify how much data is filled and how much is left. So that next time when the form gets loaded, based on the previously filled data I can identify from where to start filling the form.

I'm trying to identify at the time of saving that how many objects have values. In other words, if I find an object with all the values e.g. empty strings, I can skip that object to be saved into the database. I referred this https://stackoverflow.com/a/22683141/10037521 which shows how to check empty object which has all the string properties.

How to include boolean properties as well in this check? e.g. If the object has 10 bool properties and if all of them are false, I need to identify that object as empty.

So to summarize above question, how to identify if the object is blank which have bool or string properties?


Solution

Technically, you can combine Reflection and Linq into something like this:

  Object obj = ...

  // All Empty (false or null) public properties of either string or bool type
  PropertyInfo[] emptyProps = obj
    .GetType()
    .GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .Where(prop => prop.CanRead) // you may want to check prop.CanWrite as well
    .Where(prop => prop.PropertyType == typeof(bool) || 
                   prop.PropertyType == typeof(string))
    .Where(prop => object.Equals(prop.GetValue(obj), 
                                 prop.PropertyType.IsValueType 
                                   ? Activator.CreateInstance(prop.PropertyType) 
                                   : null))
    .ToArray();

However, I doubt if you should do this: some properties should not be saved:

   // Should be saved
   public string FirstName {...} 
   // Should be saved
   public string SecondName {...} 
   // Should NOT be saved
   public string Name {get {return $"{FirstName} {SecondName}";}}

You can have elaborated criteria which data should be saved, e.g. in case of FirstName you may want to check (at least!)

   // FirstName must be not null, not empty and not blank
   bool shouldSave = !string.IsNullOrWhiteSpace(FirstName);

That's why I suggest implementing a custom property / method within the class:

   public class MyClass {
     ...
     // virtual: you may want to add some properties in a derived class 
     public virtual bool IsEmpty {
       get {
         return string.IsNullOrWhiteSpace(FirstName) &&
                string.IsNullOrWhiteSpace(SecondName) &&
                ...
       }
     } 
   }


Answered By - Dmitry Bychenko
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing