Archive for the ‘coding’ Category

c# finding those blasted predefined IEqualityComparer classes!

Tuesday, April 14th, 2009

Why there isn’t any reference in the IEqualityComparer documentation to pre-implmented classes that override the function I don’t know.

But here’s one for string comparisons:

StringComparer

This static class contains many different predefined IEqualityComparer instances to be used in functions like “Contains”. Enjoy.

c# Getting your machine’s IP addresses.

Tuesday, March 17th, 2009

Here is the simple way to get all of the IP addresses for you machine. This code filters out everything but IPv4 address, but to truly get everything just remove the if statement.

string hostName = Dns.GetHostName();
var addrs = Dns.GetHostAddresses(hostName);
bool hasIP = false;
for (int i = 0; i < addrs.Length; i++)
{
    IPAddress addr = addrs[i];
    if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    {
        si.DataIP = addr.ToString();
        hasIP = true;
        break;
    }
}

MS SQL Delete all the data from the tables.

Friday, November 21st, 2008

Here’s a helpful, and perhaps dangerous, little tidbit of code for SQL Server 2005. It deletes all of the data from all of the tables. To be completely effective, the command may need to be run more than once because of foreign keys. Basically run it until there aren’t any errors. Then the db is clean and empty of all data.

exec sp_MSForeachTable "delete from ?"

the sp_MSForeachTable is a very useful system stored procedure.

c# linq to sql string comparisons

Monday, October 27th, 2008

There are four different string comparisons for handling string fields with linq to sql.  They are as follows:

The exact macth

DataContext dc = new DataContext(...)
var results =
  from row in dc.table
  where row.string_column == "Exact Match"
  select row;

Begins With:

DataContext dc = new DataContext(...)
var results =
  from row in dc.table
  where row.string_column.StartsWith("Ex")
  select row;

produces an sql match string “Ex%”

Contains

DataContext dc = new DataContext(...)
var results =
  from row in dc.table
  where row.string_column.Contains("Ex")
  select row;

Which is equivalent to “%Ex%”

Ends With

DataContext dc = new DataContext(...)
var results =
  from row in dc.table
  where row.string_column.EndsWith("Ex")
  select row;

Which is equivalent to “%Ex”

c# Reading the contents of a web page

Wednesday, September 10th, 2008

Reading the contents of a web page can be very useful.  Here’s some code I’m developing to read the historical stock prices from http://finance.google.com

string uri = "http://finance.google.com/finance/historical?cid=667226&startdate=Sep+9%2C+2007&enddate=Sep+10%2C+2008&output=csv";
WebRequest request = WebRequest.Create(uri);
WebResponse response = request.GetResponse();
var stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
string page = reader.ReadToEnd();

There you go. Very simple isn’t it? Just got to love .Net

c# timing functions

Thursday, June 12th, 2008

Here are some timing functions that come in the .Net framework.  I’ve used these for moderately accurate timing code — at least down to the millisecond level.  For any timing less than about 30 milliseconds these work much better than timer events piled on each other which tend to get inaccurate.

int begin = System.Environment.TickCount
... do something
int end = System.Environment.TickCount
int elapsed = end - begin;

c# getting a list of files from a directory

Wednesday, June 4th, 2008

Here’s how to get an array of file descriptors (name and other information).

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Program.DataDirectory);
System.IO.FileInfo[] files = di.GetFiles("*.txt");

c# getting the application directory.

Wednesday, June 4th, 2008

Getting the directory that the application is in at run-time is nice.  Here’s the code.

string appDir = AppDomain.CurrentDomain.BaseDirectory;

c# Mandelbrot fractal unleashed!

Thursday, May 15th, 2008

I remember the first time I saw the Mandelbrot set.  It was incredible!  After I had successfully programmed at it on my 8088 (a very old DOS computer).  I would wait 10 minutes at a time for a screen to pop up.  The detail was nothing short of amazing.  The swirls, the shapes, the colors.  The neatest thing was the ability to zoom into it to reveal more and more and more detail.

Well it’s been many years and computers have improved a great deal as have the tools to program them.  So I wrote another Mandelbrot explorer.

Mandelbrot Image

What is the Mandelbrot set? Well….  In short it is Xn+1 = Xn2 + C iterated until the magnitude of Xn is greater than 4 or the maximum number of iterations is met then the number of iterations is used as the color of the pixel.  Xn is a complex number, C is another complex number where the x coordinate of the pixel defines the real part and the y coordinate defines the imaginary part of the number. The magnitude is defined as X2 + Absolute Value of (Xi2).  Then this process is repeated for each pixel.  Holy smoke that is a mouthful!

Anyway take a peek at the code if you’d like and surely get the app and have fun exploring the Mandelbrot set.

To zoom in a spot just click on it.  The number is the number of iterations.  The more iterations the deeper the pattern goes (well until you run into percision problems).

Mandelbrot Executable

Mandelbrot source code.

Have fun!

A shift

Friday, May 9th, 2008

The last couple of weeks have been a real shift for me. After finishing up a contract I have spend most of my time in a different lanuage, and entirely different programming environment. I’ve switched from Windows and c# to Linux and Ruby. From normal applications to web applications.

One of the things I like most about switches is all the new stuff I learn. Currently I’m working on building sweetcarloan.com this has been very educational. I’ve learned a great deal about SEO (search engine optimization) and though the concepts are fairly easy to understand it’s incredible time consuming and difficult. I have new respect for those that do it for a living.

It’s been fun to dive back into Ruby a language that has some similarities to c# but some huge differences. It’s got good support for lambda statements, reflection, extensions methods, inline funtion definitions (hmm had remember the names off hand.) However there are some huge differences in syntax and Ruby is completely dynamic in typing.

So that’s this week it’s been a total shift.