Thursday, September 4, 2008

C# Ordinal Number for a Month

I was parsing this page and I was trying to convert the following date into Datetime format:

Friday 5th of September, 2008

What I thought would end up being something readily available and easy turned out not the case. The biggest issue was "September" and I had to create a procedure to get the Ordinal number with a winding routine that finally did the job for me, although have to say it was alot bigger than I expected.

To call the function it's simply:

DateTime SelDate = ConvertLongDateStr(DateStr);

Below are the routines:


private static int GetMonthsNumber(string monthname,ref bool valid)
{
for (int month = 1; month < 13; month++)
{
DateTime date = new DateTime(1900, month, 1);
if (date.ToString("MMMM").ToUpper() == (monthname.ToUpper()))
{
valid = true;
return month;
}
}
valid = false;
return 0;
}

public DateTime ConvertLongDateStr(string LongDateStr)
{
// Thursday 4th of September, 2008 //
string DoW = LongDateStr.Substring(0,LongDateStr.IndexOf(" "));
LongDateStr = LongDateStr.Remove(0, LongDateStr.IndexOf(" ")+1);
// for loop for max of 2 integers //
string daystr = "";
for (int i=0;i<=1;i++)
{
try
{
int num = Int32.Parse(LongDateStr.Substring(0,1));
daystr = daystr+LongDateStr.Substring(0,1);
LongDateStr = LongDateStr.Remove(0,1);
}
catch (Exception)
{
// nothing required here //
}

}
LongDateStr = LongDateStr.Remove(0, LongDateStr.IndexOf(" ")+1); // removes "th" "st" "rd"
LongDateStr = LongDateStr.Remove(0, LongDateStr.IndexOf(" ")+1); // removes "of"
string LongMonthStr = LongDateStr.Substring(0,LongDateStr.IndexOf(","));
bool validmonth = false;
int MonthsVal = GetMonthsNumber(LongMonthStr,ref validmonth);
LongDateStr = LongDateStr.Remove(0, LongDateStr.IndexOf(" ")+1); // removes ","
string YearStr = LongDateStr.Trim();
string ShortDateStr = daystr+"-"+MonthsVal.ToString()+"-"+YearStr;
DateTime ShortDateVal = Convert.ToDateTime(ShortDateStr);
return ShortDateVal;
}

Thursday, July 17, 2008

Start of Blog - Thread crossing C#

This is my first post for the my bog. I have been developer for over 15 years now and have seen a huge number of changes in the industry. I think most of my posts will be random hence if I can write something up here that I found difficult to solve at first and there didn't seem to be an easy to understand solution on the net.

My first post is about the unpleasant experience of upgrading one of my programs from Visual Studio 2003 to 2005. I was continually getting an exception when I was trying to update a listveiw in the main form from a thread. Due to time constraints most code I write is buggy however software needs to put out there and there is usually very little testing done at my end.

After a bit of searching I came to realise that the inital code I had written wasn't correct and I shouldn't have been calling the main form from the thread the way I did. Hence the use of these mystical items called "Delegates". I plondered for several hours trying to use the sample on the net to no avail, until one of my collegues showed me the way, thankfully!!!

Intially in my thread class I would just call the mainform listview like this:

listview1.Items[row].SubItems[column].Text = "Running ";


This failed miserably in VS2005, however the simply solution is as follows, all should be written in your thread class:

delegate void Updatelistview1Invoker(ListView listview1, int row, int column, string message);

private void MainThread_Updatelistview1(ListView listview1, int row, int column, string message)

{ listview1.Items[row].SubItems[column].Text = message; }

public void Updatelistview1(ListView listview1, int row, int column, string message)
{
listview1.Invoke(new Updatelistview1Invoker(MainThread_Updatelistview1), new object[] { listview1,row,column,message});
}

// In your code Call the following to update a particular point in your listveiw

Updatelistview1(listview1, row, column, "Running"));


Anyway I hope this helps someone out there. back to coding....