0

I am having one doubt here. After testing two if condition, it is storing only second condition value. How to store both condition value when both the condition is true. Here after executing 2nd if condition, 1st if condition value is over riding.

My model is like below.

public class TimesheetError
{
    public string Status { get; set; }
    public string Messsage { get; set; }
    public string ListErrors { get; set; }
    public bool IsNotValid { get; set; }    
}

My method is like below.

public TimesheetError Validate(Client client)
{
    TimesheetError error = new TimesheetError();

    if (client.Name.IsEmpty())
    { 
        error.Messsage = "Bad request";
        error.ListErrors = "name is not enterd";
        error.Status = "Not Found";
        error.IsNotValid = true;
    }

    if (client.Contact.FirstName.IsEmpty())
    { 
        error.Messsage = "Bad Request";
        error.ListErrors = "Contact name is not entered";
        error.Status = "Not Found";
        error.IsNotValid = true;
    }

    return error
}
Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
King
  • 53
  • 1
  • 9

6 Answers6

3

Change your method to return list of object like this

public List<TimesheetError> Validate(Client client)
    {
        List<TimesheetError> listerror=new List<TimesheetError>();
        if (client.Name.IsEmpty())
        { 
            TimesheetError error = new TimesheetError();
            error.Messsage = "Bad request";
            error.ListErrors = "name is not enterd";
            error.Status = "Not Found";
            error.IsNotValid = true;
            listerror.Add(error);
        }
         if (client.Contact.FirstName.IsEmpty())
        {
            TimesheetError error = new TimesheetError();
            error.Messsage = "Bad Request";
            error.ListErrors = "Contact name is not entered";
            error.Status = "Not Found";
            error.IsNotValid = true;
            listerror.Add(error);
        }
        return listerror
   }
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
1

Since you have only one TimesheetError you are overriding the properties in the second if-statement. If you want to store all possible errors you can use a List<TimesheetError>:

public IEnumerable<TimesheetError> Validate(Client client)
{
    var errors = new List<TimesheetError>();
    if (client.Name.IsEmpty())
    { 
        var error = new TimesheetError();
        error.Messsage = "Bad request";
        error.ListErrors = "name is not enterd";
        error.Status = "Not Found";
        error.IsNotValid = true;
        errors.Add(error);
    }
    if (client.Contact.FirstName.IsEmpty())
    { 
        var error = new TimesheetError();
        error.Messsage = "Bad Request";
        error.ListErrors = "Contact name is not entered";
        error.Status = "Not Found";
        error.IsNotValid = true;
        errors.Add(error);
    }
    return errors;
}

You can use Enumerable.Any to check if there are errors at all or use FirstOrdefault to get the first (or null if there are none). Or use a loop:

foreach(TimesheetError error in Validate(client))
{
    // ...
}

It's just a personal preference of mine to return IEnumerable<T> instead of List<T>. Why should the caller of this method be allowed to add or remove errors from the list?

Should I always return IEnumerable<T> instead of IList<T>?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Corect your message accordingly.

public TimesheetError Validate(Client client)
{

    TimesheetError error = new TimesheetError();

    if (client.Name.IsEmpty())
    { 
       error.Messsage = "Bad request";
        error.ListErrors = "name is not enterd";
        error.Status = "Not Found";
        error.IsNotValid = true;
    }
     if (client.Contact.FirstName.IsEmpty())
    { error.Messsage += " Bad Request";
        error.ListErrors += " Contact name is not entered";
        error.Status += " Not Found";
        error.IsNotValid += true;
    }
    return error
शेखर
  • 17,412
  • 13
  • 61
  • 117
Venkatesh K
  • 4,364
  • 4
  • 18
  • 26
0

I dont understand what you want if they both true the second if override the values entered in the first if, if you want to store both change your model to list of values where you want multiple values...

public class TimesheetError
{
public List<string> Statuses { get; set; }
public List<string> Messsage { get; set; }
public List<string> ListErrors { get; set; }
public bool IsNotValid { get; set; }

}
ilay zeidman
  • 2,654
  • 5
  • 23
  • 45
0

Yes your second condition will overwrite your first if condition error values because error is a reference type whose values get updated in second condition , A workaround will be to create a list of TimesheetError object , so you can add as many TimesheetError objects to your List<TimesheetError> ,

public  List<TimeSheetError> Validate(Client client)
    {
          List<TimeSheetError> error = new List<TimeSheetError>();
        //TimesheetError error = new TimesheetError();

        if (client.Name.IsEmpty())
        {  
            TimesheetError error1 = new TimesheetError();
            error1.Messsage = "Bad request";
            error1.ListErrors = "name is not enterd";
            error1.Status = "Not Found";
            error1.IsNotValid = true;
error.add(error1);
        }
         if (client.Contact.FirstName.IsEmpty())
        {
            TimesheetError error2 = new TimesheetError();       
            error2.Messsage = "Bad Request";
            error2.ListErrors = "Contact name is not entered";
            error2.Status = "Not Found";
            error2.IsNotValid = true;
       error.add(error2)
        }

        return error
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
0

If you need to return two different "Errors" from your method, you will need to return a collection of errors, for example, you will need to change your method like so:

public List<TimesheetError> Validate(Client client)
{
    ....

Then, when creating your error objects, add them to a list:

List<TimesheetError> errors = new List<TimesheetError>();

if (client.Name.IsEmpty())
{
    TimesheetError error = new TimesheetError();
    error.Messsage = "Bad request";
    error.ListErrors = "name is not enterd";
    error.Status = "Not Found";
    error.IsNotValid = true;
    errors.Add(error);
}

if (client.Contact.FirstName.IsEmpty())
{ 
    TimesheetError error = new TimesheetError();
    error.Messsage = "Bad Request";
    error.ListErrors = "Contact name is not entered";
    error.Status = "Not Found";
    error.IsNotValid = true;
}

return errors
Dave Bish
  • 19,263
  • 7
  • 46
  • 63