Tuesday, July 26, 2011

Using REGEX to Change Case Selectively

I had a list of generic medications where the names were capitalized. I needed to change the spelling to lower case, which is the accepted way to spell them.

However, converting the medication name ToLower was too simplistic, since certain capitals needed to be preserved:

abobotulinumtoxinA
alteplase, tPA
aspirin, ASA
avian influenza A (H5N1) virus vaccine

Here is the code I used:


private string ConvertStringToLC(string name)
{

foreach (Match m in Regex.Matches(name, @"[A-Z]{1}[a-z]+"))
{
name = name.Substring(0, m.Index ) + char.ToLower(name[m.Index]) + name.Substring(m.Index + 1);
}

return name;
}

Because I was updating a sql database column I also had to take care of strings that had an apostrophe in them, such as burow's solution.


drugNameNew = drugNameNew.Replace("'", "''");
 
One last thing. If the generic drug name is used at the start of sentence it needs to be capitalized.


drugNameGeneric = char.ToUpper(drugNameGeneric[0]) + drugNameGeneri.Substring(1);

Monday, July 11, 2011

How to Convert an Image in jpg or other Format to an Icon

To convert an image in jpg or other format to an Icon

Open the image in Ms Paint

Click on File -> Save As and

1. Choose “Save as Type” as 24-bit Bitmap(*.bmp)
and
2. name the file with an extention of .ico: TheFileName.ico

The image does not need to be resized (to 32 x 32 or other icon size)

Tuesday, February 1, 2011

Two C# Solutions for the FizzBuzz Problem

In case you are interested...

namespace FizzBuzz
{
class Program
{
static void Main(string[] args)
{
const string fizz = "FIZZ";
const string buzz = "BUZZ";
const string fizzbuzz = "FIZZBUZZ";

// I did this solution second
for (int i = 1; i < 101; i++)
{
string output;
if (i / 15 * 15 == i) output = fizzbuzz;
else if (i / 3 * 3 == i) output = fizz;
else if (i / 5 * 5 == i) output = buzz;
else output = i.ToString();
Console.WriteLine(output);
}

// and this solution third
for (int i = 1; i < 101; i++)
{
string output = (i / 15 * 15 == i) ? fizzbuzz : (i / 3 * 3 == i)
? fizz : (i / 5 * 5 == i) ? buzz : i.ToString();
Console.WriteLine(output);
}

Console.ReadLine();
}
}
}
// Trust me, you don't want to see the one I did first.

Tuesday, January 25, 2011

Thoughts on n-Tiered Programming

One of the problems that I had when first learning Object Oriented Programming was that the examples shown to illustrate concepts were so basic that they made no sense.

Why add piles of seemingly needless complexity when there were much easier and more straightforward ways to program solutions?

Why code interfaces that didn’t seem to add anything but more programming work and confusion?

Why create multiple Layers when all I was doing was passing unchanged values down the chain?

A lot of it started to make sense when I started doing things that were more complicated.

If the data does not change as it is passed from one layer to the next, having multiple layers doesn’t seem to make sense.

However, imagine a more usual scenario where the way your data is stored in the database does not match what is displayed in the UI.

Then it starts to get interesting.

I find it helps to think of n-tiered programming this way: The BusinessLayer translates between the data in the database and the data in the UI.

I usually have a more or less one to one correspondence between the data in the database and the objects which are created in the DataLayer and passed back and forth to the BusinessLayer.

I usually have a more or less one to one one correspondence between the data in the BusinessLayer and the data in the UI.

The BusinessLayer is where the translation happens. This is whether retrieving data from the database to display in the UI, or collecting data entered in the UI and storing it in the database.

(My other layer is an EntitiesLayer. I contains classes with properties to store the data for passing around.)

Thursday, January 6, 2011

Outlook 2007 Cannot start Microsoft Outlook. Cannot open the Outlook window

Outlook starts to open, but then gets the error in the title of this post.

Fixed by running outlook.exe /resetnavpane in the All Programs textbox:

Monday, January 3, 2011

Disable Windows7 Automatic Resize to Max

Windows 7 has a feature where, when you drag a midsize window to the edge of the screen, Windows 7 maximizes it.

I found this really annoying because I frequently have several partial windows open and I drag some over to the side so they don't block each other.

To turn this feature off:

Control Panel -> Ease of Access Center

select Make the Mouse Easier to Use

check Prevent windows from being automatically arranged...

ok to save setting