Tuesday, February 27, 2001 ..:: Home ::.. Register  Login
   TechTidBits (Blog)  

Further explanation to the ///todo listings in a previous post

Feb 25

Written by:
Wednesday, February 25, 2009 8:47 PM  RssIcon

I got some constructive feedback today from an influential reader (two actually :> SG and SB) and I thought I would quickly explain in a bit more detail the changes I made to help make Rene's life a bit easier for the ///todo management tool in a previous post.  So if you're interested, please read on! :>

I want this blog to gain a positive reputation for helping you (and me too! :>) to become a better codehead, uh....coder monkey, hhhmm.....developer!  Together as a team I truely feel we can do it, albeit a bit slower than doing it solo, but then again, where's the fun in that?! :>

Today I got some great ideas from SB and SG (thank you :>) about one of Rene's posts.  Tonight I thought I would expand a bit on the changes I made for the ///todo management tool.  The changes aren't big, earth shattering or technically complicated but most people (I learned :>) probably want small digestable tid bits without having to fire up VS just to look at a few lines of code.  You know, that's probably true more times than it's not.  I myself read a few blogs over lunch and I want that to go quickly.  So here goes, quick and to the point (yes, very much NOT like me! :>).

There are a few key pieces to support Rene's suggestion to support C++ that have file extensions with .c, .cpp, .h and .hpp.  hhhmmm that's four files, so using the Directory.GetFiles(string, string) is out of the question cause the second parameter only takes one extension for the seach pattern

So what's a coder to do then? hhhmmmm if you could put all those extensions (plus the C# one) to some datatype, see if the files in the directory have an extension that's in that variable....hhhhh....we might have something there!

Well, that's exactly what I did. I used one of the new special generic datatypes for strings called StringCollection.  Why THAT datatype?  Cause it acts like an array of strongly typed strings, I have Add, Remove, Insert, etc methods, but more important in this case I have a type of search mechanism in the Contains method!

StringCollection acceptableFileExtensions = new StringCollection();

Cool, now that we have a variable to hold the different string extensions, how do we populate it?  The most effective way is to use the AddRange method.

acceptableFileExtensions.AddRange( new string[] { ".cs", ".cpp", ".c", ".h", ".hpp" } );

You cannot use the Add method cause that only takes one string, same problem as the GetFiles().  I don't quite understand why MS had to create another method when they could have just overloaded the Add() with the string array, but hey, we have to deal with what we got right?

The next piece of magic has to do with seeing if the file's extensions are in the above variable, with StringCollection, it's quite easy, you use Contains().

foreach( string file in Directory.GetFiles( path ) )
        if( acceptableFileExtensions.Contains( Path.GetExtension( file ) ) )
          m_Files.Add( file );

The key here is the Contains takes a string (I'm using the Path object to get the extension of the file) and if the extension is included in the StringCollection, you can continue processing (in our case we add that file to be processed in another method).

That's it, yup, that's the magic to all this.  AH, OK, you say!  "You helped out Rene right?  Now help me out, add VB support!"  Sure no probs, just add the ".vb" to the AddRange method call and you're gold! :>

Grab a coffee and get coding!

Location: Blogs Parent Separator TechTidBits

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement