Twitter, .NET, C#, and LINQ

Codeplex and Google Code I’ve found has numerous projects pertaining to Twitter, my new found obsession these days.  Not sure if it will be an ongoing obsession, but having tools, code, and frameworks to work with it definitely help the prospect.

Some of the projects that I’ve found useful include;

  • LINQ to Twitter – What more could one want in a .NET world than a LINQ provider!  ROCKZ!
  • Witty – A Twitter client made with WPF.  Looks sweet, haven’t played with it enough to know if it is truly awesomeness or not.
  • GEO Twitter – I’ve been wanting to get into map & GIS related matters more.  This should enable just that.  I’ll be looking into this more over the coming weeks.

Over the next few months, along with the other fifty zillion projects I’m always working on, I think I’ll be playing with ways to filter, increase readability, and overall make twitter a bit more useful.  In addition that that I’d like to figure out a way to add twitter feeds and RSS into a useful desktop or web based application that would allow me to keep them all in one place.  Yahoo Pipes is a start, but there is a still a bit lacking, and if anything it needs a nice WPF interface for Windows Based PCs.  If anyone is interested in teaming up on some of these efforts, let me know.

Making Maths Functions n' Stuff Part 2

After completing the tests and functional methods for the other part of the solution in part 1, we’re jumping into getting the WPF application built.

First I started out by simply creating a new WPF application and renaming the default xaml form.  After renaming remember to change the StartupUri property name in the App.xaml file so things startup appropriately.

   1:  <Application x:Class="Maths.App"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      StartupUri="MathsCooking.xaml">
   5:      <Application.Resources>
   6:
   7:      </Application.Resources>
   8:  </Application>

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

I then made the following changes the the MathsCooking.xaml form and added the events below.

   1:  <Window x:Class="Maths.Window1"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      Title="Maths Cooking" Height="768" Width="1024">
   5:      <Grid>
   6:          <Button BorderThickness="3" Height="23" HorizontalAlignment="Left" Margin="332,123.638,0,0" Name="getApproximateEqualityButton" VerticalAlignment="Top" Width="150" Click="getApproximateEqualityButton_Click">Get Approximate Equality</Button>
   7:          <TextBox Height="23" HorizontalAlignment="Left" Margin="190,10,0,0" Name="textNumerator" VerticalAlignment="Top" Width="95" />
   8:          <TextBox Height="23" HorizontalAlignment="Left" Margin="190,87,0,0" Name="textValue" VerticalAlignment="Top" Width="95" />
   9:          <TextBox Height="23" HorizontalAlignment="Left" Margin="190,123,0,0" Name="textEpsilon" VerticalAlignment="Top" Width="95" />
  10:          <TextBox Height="23" HorizontalAlignment="Left" Margin="190,48,0,0" Name="textDenominator" VerticalAlignment="Top" Width="95" />
  11:          <Label Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="labelNumerator" VerticalAlignment="Top" Width="150">Numerator</Label>
  12:          <Label Height="28" HorizontalAlignment="Left" Margin="10,48,0,0" Name="labelDenominator" VerticalAlignment="Top" Width="150">Denominator</Label>
  13:          <Label Height="28" HorizontalAlignment="Left" Margin="10,123,0,0" Name="labelEpsilon" VerticalAlignment="Top" Width="150">Epsilon</Label>
  14:          <Label Height="28" HorizontalAlignment="Left" Margin="10,87,0,0" Name="labelValue" VerticalAlignment="Top" Width="150">A Value (Float or Double)</Label>
  15:          <Button BorderThickness="3" Height="23" HorizontalAlignment="Left" Margin="332,48.638,0,0" Name="getDecimalButton" VerticalAlignment="Top" Width="150" Click="getDecimalButton_Click">Get Decimal</Button>
  16:          <TextBox Height="65" HorizontalAlignment="Left" Margin="10,168,0,0" Name="textAnswers" VerticalAlignment="Top" Width="472" />
  17:      </Grid>
  18:  </Window>

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

The C# Code Behind file.  (Eventually I’ll learn all the lingo for this WPF stuff, until then, you can just deal with my WinForm speak)

   1:  #region
   2:
   3:  using System.Windows;
   4:
   5:  #endregion
   6:
   7:  namespace Maths
   8:  {
   9:      public partial class Window1 : Window
  10:      {
  11:          public Window1()
  12:          {
  13:              InitializeComponent();
  14:          }
  15:
  16:          private void getDecimalButton_Click(object sender, RoutedEventArgs e)
  17:          {
  18:          }
  19:
  20:          private void getApproximateEqualityButton_Click(object sender, RoutedEventArgs e)
  21:          {
  22:          }
  23:      }
  24:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Next in order was the effort to make the UI actually do some things.

   1:  #region
   2:
   3:  using System;
   4:  using System.Windows;
   5:  using MathCookbook;
   6:
   7:  #endregion
   8:
   9:  namespace Maths
  10:  {
  11:      public partial class Window1 : Window
  12:      {
  13:          public Window1()
  14:          {
  15:              InitializeComponent();
  16:          }
  17:
  18:          private void getDecimalButton_Click(object sender, RoutedEventArgs e)
  19:          {
  20:              textAnswers.Text = Functions.GetDecimalOfFraction(
  21:                  Convert.ToDouble(textNumerator.Text),
  22:                  Convert.ToDouble(textDenominator.Text)).ToString();
  23:          }
  24:
  25:          private void getApproximateEqualityButton_Click(object sender, RoutedEventArgs e)
  26:          {
  27:              textAnswers.Text = Functions.IsApproximatelyEqualTo(
  28:                  Convert.ToDouble(textNumerator.Text),
  29:                  Convert.ToDouble(textDenominator.Text),
  30:                  Convert.ToDouble(textValue.Text),
  31:                  Convert.ToDouble(textEpsilon.Text)).ToString();
  32:          }
  33:      }
  34:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Now we can actually try out various numbers with a UI.  Next we’ll tackle a few more additional methods and move towards some non-math related items.  I needed the Math Library refresher so I’ve hit that library first.

Making Maths Functions n' Stuff Part 1

Welcome to part 1 of my random blurbs of math stuff into methods, using TDD methods, and sticking a nice simple WPF Interface on top.  Most of these examples I dug up out of O’Reilly’s C# Cookbook.  My point below though is for TDD examples with some WPF tossed on top.  More than anything, I just wanted to do this for fun.  Without further ado, here goes the coding.

First I created two projects, one class project for the methods and one test project for the tests.

[](http://adronhall.smugmug.com/gallery/6181616_zg5Ko/1/389842739_7q6x4)

Next I jumped right in and created this test first.

   1:  #region
   2:
   3:  using MbUnit.Framework;
   4:
   5:  #endregion
   6:
   7:  namespace MathCookbookTests
   8:  {
   9:      [TestFixture]
  10:      public class Functions
  11:      {
  12:          [Test]
  13:          public void TestGettingDecimalValue()
  14:          {
  15:              const double numerator = 15;
  16:              const double denominator = 3;
  17:              const double result = 5;
  18:
  19:              Assert.AreEqual(MathCookbook.Functions.GetDecimalOfFraction(numerator, denominator), result);
  20:          }
  21:      }
  22:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Then I sketched out the method.  Of course, as always I used ReSharper to get the skeleton of the code via the ole’ Alt+Enter option.

   1:  namespace MathCookbook
   2:  {
   3:      public class Functions
   4:      {
   5:          public static double GetDecimalOfFraction(double numerator, double denominator)
   6:          {
   7:              return 0;
   8:          }
   9:      }
  10:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

This now gives me a failing test.

Now to go after the green light.

   1:          public static double GetDecimalOfFraction(double numerator, double denominator)
   2:          {
   3:              return numerator/denominator;
   4:          }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

This now gives me a green light.  Next I added two tests for the next method I wanted to add.

   1:          [Test]
   2:          public void TestApproximationIsTrue()
   3:          {
   4:              const double numerator = 1;
   5:              const double denominator = 7;
   6:              const double aValue = .1428571;
   7:              const double epsilon = .0000001;
   8:
   9:              Assert.IsTrue(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  10:          }
  11:
  12:          [Test]
  13:          public void TestApproximationIsFalse()
  14:          {
  15:              const double numerator = 1;
  16:              const double denominator = 7;
  17:              const double aValue = .142857;
  18:              const double epsilon = .0000001;
  19:
  20:              Assert.IsFalse(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  21:          }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Now of course, when I flesh out the skeleton code and just toss a default true or false back to get a clean compile, one will pass and one won’t.  But just jump into getting it to perform the appropriate function.

   1:          public static bool IsApproximatelyEqualTo(double numerator, double denominator, double value, double epsilon)
   2:          {
   3:              return Math.Abs((numerator / denominator) - value) < epsilon;
   4:          }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Now we run, and get green lights.

I wanted to enable the method above to accept floats for the value parameter also, so to do that I first added two tests.

   1:          [Test]
   2:          public void TestApproximationIsTrueWithFloat()
   3:          {
   4:              const double numerator = 1;
   5:              const double denominator = 7;
   6:              const float aValue = (float) 0.1428571;
   7:              const double epsilon = .0000001;
   8:
   9:              Assert.IsTrue(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  10:          }
  11:
  12:          [Test]
  13:          public void TestApproximationIsFalseWithFloat()
  14:          {
  15:              const double numerator = 1;
  16:              const double denominator = 7;
  17:              const float  aValue = (float) 0.142857;
  18:              const double epsilon = .0000001;
  19:
  20:              Assert.IsFalse(MathCookbook.Functions.IsApproximatelyEqualTo(numerator, denominator, aValue, epsilon));
  21:          }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Then I got into doing two things, one adding the new method and the second thing was doing a complete refactor of the code.  What I ended up getting green lights with was the code below.

   1:          public static bool IsApproximatelyEqualTo(double numerator, double denominator, double value, double epsilon)
   2:          {
   3:              return IsApproximatelyEqual(numerator, denominator, value, epsilon);
   4:          }
   5:
   6:          public static bool IsApproximatelyEqualTo(double numerator, double denominator, float value, double epsilon)
   7:          {
   8:              return IsApproximatelyEqual(numerator, denominator, value, epsilon);
   9:          }
  10:
  11:          private static bool IsApproximatelyEqual(double numerator, double denominator, object value, double epsilon)
  12:          {
  13:              if (value is double)
  14:              {
  15:                  return Math.Abs((numerator / denominator) - (double)value) < epsilon;
  16:              }
  17:              return Math.Abs((numerator / denominator) - (float)value) < epsilon;
  18:          }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

So that’s it for now.  I’ll be hitting on more Maths Cookbook material, and will eventually get on to some other topics related to security, threading, delegates, and other material.  Eventually I’ll get around to posting some basic pattern implementations and how to test various parts of those patterns.  Basically this is kind of a little series I’m starting for my own entertainment, but maybe it might be useful to some out there in the development community.

In part two I’ll add the WPF UI to the application so we can actually enter and play with different values.

Microsoft Rant

[Rant On]

I’m very much pro standards.  I’m pro rail standards, I’m pro road standards, I dig web standards and browser standards, and I enjoy security standards and all sorts of standards.

Now, when there are standards and some entity ignores the standards it really pisses me off.  Recently I’m trying to download various WCF Projects from Microsoft and they ask for authentication via one’s passport.  Of course I have that and tried to login with it.  I was using Chrome and it didn’t work.  What makes this even worse is the fact that the little AJAX busy icon came up and I got zilch.  No message, no notification, no communication in any way that the site was stuck.  This is complete crap, and Microsoft should fix this immediately.  I tried a couple months ago and this even happened in IE.  Other problem being I’ve only got this to work in IE sometimes and also in Firefox… sometimes.  Opera it seems to work in consistently, which is odd.

[Rant Off]

Anyway, I’m done ranting now, on to a new topic…   which I have no idea for yet but I am actively researching WCF technology and authentication practices right now.  With that research I’ll definitely have a few blog entries coming down the pipe soon.

Stay Tuned