Archive for category .Net

Regular expressions and Visual Studio

It can take a while to get to know how to use regular expressions well. After all most of us are used to

*.*

as our wild card language. It’s the old DOS style filtering that’s still used in windows to this day. Or perhaps you’re more used to SQL wild cards, eg:

‘%some text%’

Well, just because we didn’t have enough wild card standards now there’s regular expressions! They look more like:

.*\..*
^.*[a-zA-Z0-9 ]*
etc

Regular expressions, though they do come with a bit of a learning curve, are actually a really great improvement on wild card languages. The biggest problem I’ve had with them so far is that the standard is not completely solid. The way they work in .Net, Java, Perl is very similar, but not exact! This can lead to some huge headaches when trying to write / port them.

One of the greatest features of Regular expressions is it’s ability to search and replace strings in files. If you’ve ever had a strange pattern that you needed to search for and didn’t know how, that’s where RegExp would come in.

Say you had a whole bunch of html that had href’s that looked like this:

<a href="/page?queryItem=1234">content</a>
<a href="/page?queryItem=125">content</a>
<a href="/page?queryItem=3456">content</a>
<a href="/page?queryItem=1289834">content</a>

and you were given a project that required those url’s change from
/page?queryItem=1234
to
/queryItem=1234.html
you would have allot of work on your hands! Well not any more. In Visual studio you can do a search and replace like this:
Find what:
/page?queryItem={[0-9]*}
Replace with:
/queryItem=\1\.html

What is happening here is the curly brackets {} are creating groups. These groups are numbered in the order they show up in the RegExp so the replace text is all just hardcoded text with two exceptions:
1) \1 this means use the content found in group 1 from the find
2) \. this is just the escape sequence for a period. A period in a RegExp means “any character” and must be escaped if you actually need a period.

What if you wanted to do it in another Regular expression parser you say? Well, it depends on the parser. I’ve found that the same replacement in another parser I regularly use requires the grouping not use curly braces {}, but round ones (). So the replacement would change to:

Find what:
/page?queryItem=([0-9]*)
Replace with:
/queryItem=\1\.html

That’s the only difference, of course it matters what your Regular expression contains. Some parsers require

[a-zA-Z]

if you want both upper and lower case, other parser allow one and call it case insensitive. In that case

[a-z]
and
[A-Z]

are the same thing.

Best of luck with your Regular expressions.

Good reference:
http://www.regular-expressions.info/

Leave a comment

Recursion

If you’re new to programming you may not know what Recursion is.  Think file / folder structure in windows.  If you had to find every file in a folder and all it’s sub folders how would you do it?  Answer: recursion.

Here’s a handy little pattern snippit if you’re looking to do recursion on files / folders in .net:

        static void SomeRecursiveFunction(string sFolderPath)
        {
            foreach (string sFile in System.IO.Directory.EnumerateFiles(sFolderPath))
            {
                //Do work here
            }
            
            foreach (string sDir in System.IO.Directory.EnumerateDirectories(sFolderPath))
            {
                SomeRecursiveFunction(sDir);
            }
        }

Leave a comment

Math.Pow is annoying – so I wrote my own

If you’ve not had to use it yet, Math.Pow() is annoying.

What it does: finds the ‘Power of’ x. So 2^2=4, 2^4=16, etc.

Why it’s annoying:

Well, there are a number of reasons. The first is it’s not the ^ symbol which is widely used by many programming languages (but not .Net).

The second is it requires decimal as it’s input type. Why MS? Why? I wanted to use the ulong datatype in my project but couldn’t get the power of it without converting it to decimal first. With large numbers this has the annoying feature of being expressed with .Nets exponential syntax like so:
1.2345678901230000E-030

I was working on a project that required me to calculate very large whole numbers and then write them to a file, this was an issue.

So, what I did was make it better by re-writing Math.Pow using ulong. I’m sure there are other ways to do this, but re-writing this function proved really simple to do. Feel free to use my method in your projects (though leaving a reference in the code to me would be nice!), also feel free to post alternative ways to solve this problem. I always enjoy your feedback!

//Pow() taken from Anthony's Tech Blog!
//https://anthonystechblog.wordpress.com/2011/04/18/math-pow-is-annoying/
        static ulong Pow (ulong x, ulong y)
        {
            if (y == 0) return 1;
            ulong count = x;
            for (ulong i = 0; i < y-1; i++)
            {
                count = x * count;
            }
            return count;
        }

Leave a comment

Lambda Expressions

Lambda Expressions are a great way to simplify code that uses delegates.  I won’t bother getting to deep into it as this site does a great job of explaining the concept:

The Blomsma Code: Three steps to understanding Lambda expressions in C#

Leave a comment

Linq – what it is

Linq (Language Integrated Query) is a great new way of fetching data from a database.  If you’ve ever written a data driven application then you know the pain of writing SQL to interact with your database.  It’s always seemed clunky to me, here you are creating objects with properties but the way those objects get saved to a database and read back out is in a language completely different from what you program in.

Linq is an attempt to change that.  Where a standard query looks like this:

Select col1, col2, col3 from myTable where col1='this'

a Linq query is written in your code and looks like this:

     var vProducts =
        from p in products
        where p.UnitsInStock > 0
        select p;

To view more Linq examples this is a great resource:
101 LINQ Samples: http://msdn.microsoft.com/en-us/vcsharp/aa336746

Leave a comment

WPF – Working with Listboxes

This is a pretty good article on how to create a list box.  What I like about it is it starts off really simple and works towards more advanced features.  It’s pretty easy to follow allong with:

http://www.c-sharpcorner.com/UploadFile/mahesh/WPFListBox08252008100659AM/WPFListBox.aspx

Leave a comment

WPF – Windows Presentation Foundation

I’ve been dabbling with WPF on a on again off again basis for a while now.  For those of you that have yet to grasp the concept this is a pretty good read on the concept:

http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/432/MVVM-for-Tarded-Folks-Like-Me-or-MVVM-and-What-it-Means-to-Me.aspx

Leave a comment