C#
if you have been developing more than a few weeks it will be made apparent that you can’t sort a dictionary because of obvious reasons which I won’t go into,all you need to is that its by design and not a bug. therefore, here are some simple ways you can possible sort a dictionary. lets populate a dictionary with some data. // Populate some data into a dictionary
Dictionary<string, int> d = new Dictionary<string, int>();
for (int i = 0; i < 100; i++)
...
Here is a simple way to get the week number given a certain date and time. int week = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
For some unknown reason you can’t do a TryParse on nullable types or should I say the out parameter of a TryParse can not be nullable? For example this just won’t work. DateTime? dt;
DateTime.TryParse("12/12/2010", out dt);
Error 4 The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
The solution or workaround is to use two variables to accomplish the goal, here is an example.
DateTime? dt1;
DateTime dt2;
dt1 = DateTime.TryParse("12/12/2010", out dt2) ? dt2 : (DateTime?)null;
Before I start cramming code, I’ll like to know what you guys think. I’ve been working heavily on my key value store and it has been working as expected, however, the main reason for me building this store does not seem to be completely eliminated with the functionalities that I have in place so far. therefore, I’ll like to add an extra piece to the jigsaw but before I do so I’ll like to hear what you have to say. The storage engine itself is solid and it works with the minimal functionalities that it currently has. As a...
As I stated in my previous posts here and here, I have set out to write my own NoSQL database simply because I can – well not really but because it will give me the edge, learning experience will be great and there isn’t really too many options when it comes to native .NET NoSQL Databases. I want to be able to build web applications without the RDBMS overhead and schema design time. I haven’t done much yet with distributed or replicating side of things yet, so far all I wanted was a prototype to show that it...
In the old days, we have to manually create custom configuration sections if we want to do cool stuff with app/web.config or simply just put the values in the appSettings section. However, there are a few config options that are out there that most developers don’t know about, a perfect example is the mailSettings config section which allows you to setup your mail settings in your configuration file and simply call the SMTPClient to send an email and by doing so, changing settings can be done on the fly and oh’ if you are worried about security, don’t forget you...
Here is a very simple way to resize an image using a bitmap as a canvas and the graphics object as a the resizing tool. Don’t forget to reference System.Drawing. /// <summary>
/// Resizes an image from a source file to a destination file
/// the destination file will contain the exact image at the size specified
/// </summary>
/// <param name="sourceImagePath">the source image file that needs to be resized</param>
/// <param name="destinationImagePath">the destination path that the source
/// image file needs to be resized to</param>
/// <param name="width">the new width</param>
/// <param name="height">the new height</param>
static void ResizeImage(string sourceImagePath,
...
One of the most efficient and non-intrusive ways you can implement an intercepting procedure is via an HttpModule. An HttpModule simply receives every request going to or coming from IIS and it allows you to manipulate the content as you see fit and pass it along. I wanted to spit out the time it takes from receiving a request to writing the response on one or more web pages and there was no other better way of doing it except writing an HttpModule, it toke me all but 10mins. Here is the code, feel free to blow it up. ...
/// <summary>
/// using reflection to create an instance of a generic type
/// </summary>
/// <returns>T as the generic type</returns>
private static T GetNewObject()
{
try
{
return (T)typeof(T).GetConstructor(new Type[] { }).Invoke(new object[] { });
}
catch...
Once you start building applications that run every so often for an undefined/unknown amount of time, its very possible that two processes may overlap. For example, if you have a schedule task that executes a console application every 5 mins, it is possible that very much possible that it will execute another before the previous one completes. So, how do we determine if a process is already running? So far I've seen three ways of doing it. Use the Singleton pattern Use Mutex Use System.Diagnostics (my preffered method) /// <summary>
/// Determines if this process is already running, if so it will kill itself.
/// </summary>
static void ExitIfRunning()
{
...
Full C# Archive
Next entries »