none
How to check if directory is empty?

    Question

  • And in the constructor i have

    bool

     

     

    .TryParse(setting_file.GetKey("Voices").Trim(), out b);

    And if the setting_file is empty lets say it was created some seconds ago and empty and the user run the program im getting null exception on this bool.TryParse line. How to fix it? How to check if its null?

    Thanks.


    danieli
    Sunday, January 02, 2011 6:45 PM

Answers

  • Hi,

    I would simply split the line up.

    So you could build it up to:

          if (settings_file == null)
            return; // Do whatever you want to do here
    
          var key = settings_file.GetKey("Voices");
          if (key == null)
            return; // Do whatever you want to do here
    
          bool.TryParse(key.Trim(), out b);

    That way you have all null checks that might occur. Of course: You should change the return command with something meaningfull in you application.

    I hope I got you right and was able to help you.

    With kind regards,

    Konrad

    • Marked as answer by chocolade Sunday, January 02, 2011 7:31 PM
    Sunday, January 02, 2011 7:03 PM

All replies

  • Hi,

    I would simply split the line up.

    So you could build it up to:

          if (settings_file == null)
            return; // Do whatever you want to do here
    
          var key = settings_file.GetKey("Voices");
          if (key == null)
            return; // Do whatever you want to do here
    
          bool.TryParse(key.Trim(), out b);

    That way you have all null checks that might occur. Of course: You should change the return command with something meaningfull in you application.

    I hope I got you right and was able to help you.

    With kind regards,

    Konrad

    • Marked as answer by chocolade Sunday, January 02, 2011 7:31 PM
    Sunday, January 02, 2011 7:03 PM
  • Hi,

    In general you can have something like:

    bool b;
    string key;
    if(null != setting_file && !string.IsNullOrEmpty(key = setting_file.GetKey("Voices").Trim()))
     bool.TryParse(key, out b);
    

    Regards,

    Yasser.


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    Sunday, January 02, 2011 7:08 PM