Question

Helixpoint on Wed, 26 Jul 2017 15:35:02


        private void checkRequired(Itcr tmp, string[] reqFields)
        {
            if (reqFields.Any(str => string.IsNullOrEmpty(str)))
            {
                throw new System.InvalidOperationException("A required field is missing");
            }            
        }


www.helixpoint.com


Sponsored



Replies

RJP1973 on Wed, 26 Jul 2017 15:45:41


First, you should learn how to use the Visual Studio debugger. You could put a breakpoint on the line that throws the exception and then run the app from Visual Studio.

Second, your test is simply checking to see if any of the strings in the reqFields string array are null or empty. If they are then the exception is thrown.

Is that your intent?

Helixpoint on Wed, 26 Jul 2017 15:51:30


My intention was to check if any value in the string array was empty, but now they want to see which is empty

DA924x on Wed, 26 Jul 2017 16:58:22


My intention was to check if any value in the string array was empty, but now they want to see which is empty

www.helixpoint.com

Well, one doesn't go around throwing exceptions on basically a validation check.

https://msdn.microsoft.com/en-us/library/ms229009(v=vs.100).aspx

feih-7 on Thu, 27 Jul 2017 06:17:07


Hello Helixpoint´╝î

The Any method just Determines whether any element of a sequence exists or satisfies a condition . And can't pick one that not satisfies a condition . If you want to see which is empty , I suggest you can follow  the demo as below.

      private static void checkRequired(string[] reqFields)
        {
            for (int i = 0; i < reqFields.Length; i++)
            {
                if (string.IsNullOrEmpty(reqFields[i]))
                {
                    Console.WriteLine("the index {0} of  array is empty", i);
                }
            }         
        }

Best regards´╝î
feih_7

Viorel_ on Thu, 27 Jul 2017 07:16:18


If you are interested in all of the indices, then try this:

string s = string.Join( ", ",
              reqFields
                 .Select( ( v, i ) => new { v, i } )
                 .Where( p => string.IsNullOrEmpty( p.v ) )
                 .Select( p => p.i )
           );

if( s.Length != 0 )
{
   throw new ArgumentException( $"The following elements of {nameof( reqFields )} are missing: [{s}]" );
}


CoolDadTx on Thu, 27 Jul 2017 13:56:45


This seems like a good candidate for IValidatableObject. It seems like you are trying to validate that the object is valid. You're using an exception right now but the interface is a better option. Most modern UI technologies support it so you can actually bubble the error up.

class Program
{
    static void Main ( string[] args )
    {
        var items = new Itcr[]
        {
            new Itcr() { Name = "1", RequiredFields = new [] { "Field1" } },
            new Itcr() { Name = "2", RequiredFields = new [] { "Field1", "" } },
        };

        var results = new Collection<ValidationResult>();
        foreach (var item in items)
        {
            results.Clear();
            if (!Validator.TryValidateObject(item, new ValidationContext(item), results))
                Console.WriteLine($"Itcr {item.Name} is missing fields");
        };
    }
}

class Itcr : IValidatableObject
{
    public string Name { get; set; }

    public string[] RequiredFields { get; set; }

    public IEnumerable<ValidationResult> Validate ( ValidationContext validationContext )
    {
        //Should no required fields fail validation?
        if (RequiredFields?.Any(s => String.IsNullOrEmpty(s)) ?? false)
            yield return new ValidationResult("One or more required fields is missing");
    }
}

But your requirements seem a little off to me. Firstly you are wanting the value(s) that throw the exception but the check is for an empty string so all the value(s) you got would be empty strings. Seems like you want the Itcr value instead. This isn't a problem for the method you posted but for the caller of the method. 

Secondly, if you don't want to allow empty strings in the required field then I wouldn't even bother exposing it as an array. I'd personally create a derived collection that accepts strings. Then I'd override the Add/Insert method to throw an exception if the value is null or empty. This eliminates the need for validation and it will fail at the point of insertion. If you're getting the list of strings from an external source then catch the error while processing that list, not when associating it with (or after) the Itcr object.

Michael Taylor
http://www.michaeltaylorp3.net