Archive

Archive for the ‘.NET’ Category

Using LINQ to process RegEx matches

December 12th, 2008

Today I used LINQ to help implement a cleaner solution to a problem I was trying to solve. It’s really just some syntactic sugar and a chance to use a buzzword (LINQ).

My simple data class:

?Download download.txt
1
2
3
4
5
public class ReturnRecord
{
    public String FieldKey;
    public String FieldValue;
}

Basically, I needed to return an array with the key/value pairs found in a text document (of type ReturnRecord[]). RegEx was my obvious choice for parsing the text and finding the key value pairs. The keys and values will be decorated with #’s in the document.
Sample text:

?Download download.txt
1
String text = "Blah blah #key#value# blah blah.";

Here is my RegEx setup:

?Download download.txt
1
2
Regex regex = new Regex("#(.+?)#(.+?)#");
MatchCollection keyvalues = regex.Matches(text);

Now I can use a nifty LINQ query to create the array for me without having to write the usual for-loop plumbing. As you can see, LINQ creates the objects and initializes them for me in a single query.

?Download download.txt
1
2
3
4
5
6
7
8
9
List<returnrow> results;
if (keyvalues.Count > 0)
{
results = (from Match m in keyvalues
select new ReturnRow
   { FieldKey = m.Groups[1].Value, 
     FieldValue = m.Groups[2].Value }
     ).ToList();
}</returnrow>

All that’s left is to return

?Download download.txt
1
results.ToArray()

and I’m done. It’s that much less for me to maintain.

MSDN has some examples as well http://msdn.microsoft.com/en-us/library/bb882639.aspx or, check out this post http://schotime.net/index.php/2008/03/10/linq-and-regular-expressions/.

.NET , ,

5 reasons Silverlight will p0wn Flash

December 2nd, 2008

Silverlight is about rich internet applications (RIA).

Let’s define a few terms before we get started. First, I define p0wn as 95%+ plugin saturation, all the annoying ad companies using it to get in your face (say hello to Silverlight fullscreen), and it becomes the choice for RIA development. Second, Silverlight is Silverlight 2.0. I’ll try to distinguish between each version of Flash.

Now a little background on where I’m coming from with Flash. I’ve used Flash versions 4-8. These days I’m using Adobe Flash 8 and these comparisons are based on version 8 with Actionscript 2.0. Since I primarily work in Actionscript I’m going to say that the designer parts of the applications are fairly equal. Begin flame wars.

#1 Works for designers AND developers

As a developer, I can fire up Visual Studio and start coding up in my favorite .NET language. Full Intellisense in code-behind and XAML, powerful debugger, all the comforts of home. Traditional designers have to option to use Expression Studio to create their interfaces of glory. Silverlight isn’t built for the designer with a hasty tack-on for the developer.

#2 Silverlight uses real languages

C# and VB are more than just a scripting language. Silverlight is running in mature languages and on a mature framework. This doesn’t require a Silverlight specific skill set. Microsoft has been in the software development and language development business pretty much forever. You also have the advantage of all the .NET content that exists. There is a preexisting devlopment support structure. This isn’t a new language or tool set that no one has ever seen or used before and it’s up to you to find your own path through the abyss.

#3 You can actually debug your code

To put it simply, in Adobe Flash 8, debugging is a joke (an extremely unfunny joke). This is the reason I’m writing this post. I needed to debug a simple problem. It was like the old days of trying to debug javascript with alert boxes. I’m going to download a trial of Flash CS3 just to see what the debugging looks like. Support for intellisense is very poor and almost non-existent. Really, the whole code-editing experience is bad. You have to run it to see if you have any syntax errors.

#4 Develop Silverlight for FREE

That’s right folks, you can develop Silverlight for FREE. I’ll try to have a follow-up post about this. You can download Visual Studio Express for free. The Silverlight tools are free. Expression Studio is not free, but it’s not required to develop Silverlight applications. Keep in mind that you don’t need any special hosting for Silverlight either.

#5 Do you really need a fifth?

I lied. I only have 4 reasons. Seriously, do you really need any more? Go, download, develop. Live the awesomeness.

So is this the end of Flash? Of course not, silly. The Flash plugin has 95%+ saturation. It’s at version 10 so they’ve been in the game a long time. They have a large, active base of support. I’m willing to bet they even have a few fanboys. The issues I have with it crop up because it’s a designer’s tool first and we’re asked to develop applications with it. Flex is an attempt to address these issues.

When Flash first came out it was considered a toy. No body knew where it was going to go. Silverlight has been released as a serious tool to develop applications with.

Also, I think Microsoft needs the competition to continue to innovate. Flash CS4 has something called bones that allow objects to be connected to each other and move in relation to each other on the joints. That’s pretty sweet. Silverlight has Deep Zoom.

Flash makes movies and it has been used to make applications. Silverlight is for making applications and you could probably make movies with it. Silverlight isn’t set to take over the fancy animation market. Silverlight is for doing stuff.

.NET , , , ,