Wednesday, June 27, 2007

Useful .Net stuff often overlooked

Ok, I need to write it down somewhere, the article has a lot of explanations, and I just want to record classes because I know what they are, but I will forget later.

The article is here: http://haacked.com/archive/2007/06/13/the-most-useful-.net-utility-classes-developers-tend-to-reinvent.aspx

Stuff to remember:

Combine path:
public string GetFullPath(string filename)
{
string folder = ConfigurationManager.AppSettings["somefolder"];
return System.IO.Path.Combine(folder, filename);
}
Get folder location:
someFolderPath + Path.DirectorySeparatorChar;
Get just the file name:
string fileName = Path.GetFileName(fullPath);
Get absolute path from virtual:
string path = VirtualPathUtility.ToAbsolutePath("~/Controls/Test.ascx");
Get new line separator (really neat one!) Environment.NewLine;

From comments:

Check if var is a number: Double.TryParse or Int.TryParse

Timing execution (not sure, but that's what it sounds like from the comment):
System.Diagnostics.Stopwatch

Query string: HttpUtility.ParseQueryString
Raw URL: HttpContext.Current.Request.RawUrl; (MSDN)

Labels:

Monday, June 04, 2007

I am the megamosk

Calculate average of up to three scores (presence of a score is determined by the gradeN>0 condition).

CASE WHEN Topic1>0 THEN Grade1 +
CASE WHEN Topic2>0 THEN Grade2 +
CASE WHEN Topic3>0 THEN Grade3
ELSE 0 END
WHEN Topic3>0 THEN Grade3
ELSE 0 END
ELSE
CASE WHEN Topic2>0 THEN Grade2 +
CASE WHEN Topic3>0 THEN Grade3
ELSE 0 END
WHEN Topic3 > 0 THEN Grade3
ELSE 0 END
END AS TotalGrades
, CASE WHEN Topic1>0 THEN 1+
CASE WHEN Topic2>0 THEN 1+
CASE WHEN Topic3>0 THEN 1
ELSE 0 END
WHEN Topic3>0 THEN 1
ELSE 0 END
ELSE
CASE WHEN Topic2>0 THEN 1+
CASE WHEN Topic3>0 THEN 1
ELSE 0 END
WHEN Topic3>0 THEN 1
ELSE 0 END
END AS NumberOfTopics

Unfortunately, I do not know how to calculate the actual average (TotalGrades/NumberOfTopics), so I guess I am not the megamosk. Oh well...

Labels: