Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public class Parameter
{
  public int Id {get;set;}
  public string name {get;set;}
  public List<Phonenumbers> number {get;set;}
}

public class Phonenumbers
{
  public string number1 {get;set;}
  public string number2 {get;set;}
}


public Task<bool> Content(Parameter par)
{
  if(string.IsNullOrWhiteSpace(par.Id).ToString()))
    {
      //Todo
    }
 if(string.IsNullOrWhiteSpace(par.name))
    {
      //Todo 
    }
   
//I want to also validate List contents (number1,number2) fields if IsNullOrWhiteSpace
}


What I have tried:

<pre>

public class Parameter
{
  public int Id {get;set;}
  public string name {get;set;}
  public List<Phonenumbers> number {get;set}
}

public class Phonenumbers
{
  public string number1 {get;set;}
  public string number2 {get;set;}
}


public Task<bool> Content(Parameter par)
{
  if(string.IsNullOrWhiteSpace(par.Id).ToString()))
    {
      //Todo
    }
 if(string.IsNullOrWhiteSpace(par.name))
    {
      //Todo 
    }
 if(string.IsNullOrWhiteSpace(par.Phonenumbers.Any(x => x.number1== null).ToString()))
    {
      //Todo 
    }
}
Posted
Updated 23-Jul-18 6:08am

I would change your code here slightly. I have changed the return type here to just a bool as I am not sure what the Task<bool> is doing here

C#
public bool Content(Parameter par)
{
  if(par != null)
  {
    //int can not be null but it can have a default number
    //so test for that, unless the type is int? then you can 
    //test for null directly
    if(par.Id != 0)
    {
       //Todo
    }

    if(String.IsNullOrWhileSpave(par.name))
    {
      //Todo
    }

    if(par.Phonenumbers != null)
    {
      var failed = par.Phonenumbers.Count(p=> String.IsNullOrEmpty(p.number1) || String.IsNullOrEmpty(p.number2));
      if(failed > 0) 
      {
        //todo
      }
    }
  }
}
 
Share this answer
 
Comments
wizklaus 23-Jul-18 13:33pm    
Thanks for your solution. it has helped in solving it. as for the Task<bool>, my project is of asynchronous methods where i await the result.
There is no point in converting the Id field from int to string - it will default to 0 if you don't set it. Therefore you should just check that it is within a valid range (e.g. greater than 0).

You need to check that the par object is not null before attempted to access the numbers property. Otherwise you will get a Null Reference Exception when the object is null.
Also if the count of numbers is 0 then it will not find any phone numbers.


// Int will always be populated (it defaults to 0 if you don't set it).
if (par.Id <= 0)
{
    Console.WriteLine("1");
}
if (string.IsNullOrWhiteSpace(par.name))
{
    Console.WriteLine("2");
}
if (par.number == null ||
    par.number.Count == 0 ||
    par.number.Any(x => x.number1 != null || x.number2 != null)
    )
{
    Console.WriteLine("3");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900