-2

I'm not sure how I could incorporate my own method into code using isNullorWhiteSpace, my framework isn't 4.0. I've had some help previously and they suggested using isnullorwhitespace, is it not the most preferred method to display:

2/20/2014 7:33:10 AM, MEASURED VELOCITY: 0.2225

I can't seem to find equivalent code that will work.

 using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;

    namespace conApp
    {
        class Program
        {
            public static class StringExtensions
            {
                public static bool IsNullOrWhiteSpace(string value)
                {
                    if (value != null)
                    {
                        for (int i = 0; i < value.Length; i++)
                        {
                            if (!char.IsWhiteSpace(value[i]))
                            {
                                return false;
                            }
                        }
                    }
                    return true;
                }
            }

            static void Main(string[] args)
            {
                String line;
                try
                {
                    using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.txt"))
                    {
                        string mydirpath = "C:\\chat\\";

                        string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

                        foreach (string txtName in txtFileList)
                        {
                            System.IO.StreamReader sr = new System.IO.StreamReader(txtName);

                            while ((line = sr.ReadLine()) != null)
                            {
                                String spart = ".prt";
                                String sam = " AM";
                                String spm = " PM";
                                String sresult = "TEST RESULT: ";
                                String svelocity = "MEASURED VELOCITY: ";
                                String part = string.Empty;
                                String date = string.Empty;
                                String result = string.Empty;
                                String velocity = string.Empty;
                                // sw.WriteLine(line);

                                    if (line.Contains(sam) || line.Contains(spm))
                                    {
                                        date = line;
                                    }

                                    if (line.Contains(spart))
                                      {
                                           part = line;
                                      }

                                    if (line.Contains(sresult))
                                      {
                                          result = line;
                                      }

                                    if (line.Contains(svelocity))
                                      {
                                          velocity = line;
                                      }

                                  if (!String.IsNullOrWhiteSpace(date) && !String.IsNullOrWhiteSpace(velocity))
                                  {
                               bool isNullOrWhiteSpace = "foo bar".IsNullOrWhiteSpace(); //doesnt work here

                                      int I = 2;
                                      string[] x = new string[I];
                                      x[0] = date;
                                      x[1] = velocity;
                                      sw.WriteLine(x[0] + "," + x[1]);
                                  }



                            }

                        }
                    }
                }
                catch
                {

                }
            }

        }
    }
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
tedTosterone
  • 35
  • 1
  • 8
  • I already replaced with equivalent code in [my answer](http://stackoverflow.com/a/30647369/4904830). `if (!string.IsNullOrEmpty(line) && line.Trim().Length != 0)` – Blas Soriano Jun 04 '15 at 16:25
  • I'm not sure what you have problem with. Post have huge amount of code - try to cut it down... Also spelling method name wrong in title does not make your post stand out as good one... – Alexei Levenkov Jun 04 '15 at 16:28
  • 1
    Are you looking for "how to write extension method for string datatype" (i.e. http://stackoverflow.com/questions/2763968/c-sharp-extension-method-for-string-data-type )? – Alexei Levenkov Jun 04 '15 at 16:29

1 Answers1

5

Firstly, StringExtensions needs to be a top level class, so it cannot be inside another class.
Secondly, you need to turn the method into an extension method by adding the this keyword to the first parameter:

public static bool IsNullOrWhiteSpace(this string value)

So it becomes:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value != null) {
            for (int i = 0; i < value.Length; i++) {
                if (!char.IsWhiteSpace(value[i])) {
                    return false;
                }
            }
        }
        return true;
    }
}

class Program
{
    ...
}
Dennis_E
  • 8,751
  • 23
  • 29