0

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?

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
Manish Dhariwal
  • 61
  • 1
  • 1
  • 9
  • 2
    But is "false" also a valid value? Would `bool?` be better? – ProgrammingLlama Jul 13 '18 at 08:09
  • Why don't you just store which "step" which the user was at? – Sweeper Jul 13 '18 at 08:12
  • @john : I'd consider this as a very good suggestion so that we can handle both true/false as non empty. Agreed. I'm open to make that change. Thank you. – Manish Dhariwal Jul 13 '18 at 08:13
  • @Sweeper : I can not due to structure of the application. I have to identify it using the data. – Manish Dhariwal Jul 13 '18 at 08:13
  • For non-strings, you should probably get the default value for the given type. See [this answer](https://stackoverflow.com/a/2490274/3181933) for how to do it. – ProgrammingLlama Jul 13 '18 at 08:14
  • 2
    Use nullable variables. Null means blank. You can have nullable bool too – Alexandru Pupsa Jul 13 '18 at 08:21
  • As @AlexandruPupsa states, nullable vars could help - but I'd probably create a base class which can track whether a user has 'done' it (e.g. pressed the 'Next' button after being presented with the field/page) - this way you could have things that are genuinely optional and skipped but still 'completed' – GPW Jul 13 '18 at 08:28

3 Answers3

2

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) &&
                ...
       }
     } 
   }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

You may serialize it to a json then test against the string to achieve a similar checking:

public static object? NullIfEmptyValues(object obj) =>
    JsonSerializer.Serialize(obj) is "\"\"" or "[]" or "null" ? null : obj;
n0099
  • 520
  • 1
  • 4
  • 12
0

You can check it with a simple check:

obj == null || obj == String.Empty

This will check for both null and "empty" conditions.

zcoop98
  • 2,590
  • 1
  • 18
  • 31