Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, December 12, 2009

Weekly Source Code: C# DateTime Developer Pitfall

Base upon my previous blog post that every language has some pitfalls I had a production issue and saw one of the pitfalls that C# have with the DateTime class which is part of the .Net framework.

I renamed this title since its more a pitfall of the developer not understanding the proper use of the DateTime class. The all point is that your telling the constructor to create a date, you don't tell it to work with increments.

In the next code you want to get the 1ste date of the following month:

DateTime currentDate = new DateTime(2009, 11, 05);
DateTime nextDate = new DateTime(currentDate.Year, currentDate.AddMonths(1).Month, 1);
Console.WriteLine("nextDate: " + nextDate.ToString("dd/MM/yyyy"));

So the answer is 01/12/2009 which is correct but what if you do the following:

DateTime currentDate = new DateTime(2009, 12, 05);
DateTime nextDate = new DateTime(currentDate.Year, currentDate.AddMonths(1).Month, 1);
Console.WriteLine("nextDate: " + nextDate.ToString("dd/MM/yyyy"));

Code is exactly the same, I just change the month to December since you want to get the day for the 1ste of Jan 2010 but the end result is: 01/01/2009. Ouch!!

Yes the DateTime object doesn't increment the year (2009 -> 2010).

To fix:

DateTime currentDate = new DateTime(2009, 12, 05);
DateTime nextDate = new DateTime(currentDate.Year, currentDate.Month, 1);
nextDate = nextDate.AddMonths(1);
Console.WriteLine("nextDate: " + nextDate.ToString("dd/MM/yyyy"));

You see that you can't increment in a DateTime.

Thursday, October 22, 2009

Follow-Up: Make Web Service Configurable

Base upon the comments of a reader on my last weekly source code posting is that there is an additional 2x ways to set the URL location of a Web Service:

string newUrl = "http://server/path/to.asmx";
MyService service = new MyService();
service.Url = newUrl;

Or in new C# 3.0 you can use object initializers:

string newUrl = "http://server/path/to.asmx";
MyService service = new MyService {Url = newUrl};

I still prefer using a constructor.

Tuesday, October 20, 2009

Weekly Source Code: Make Web Service Configurable

Sorry for the delay, been crazy at work.

In Visual Studio.Net you call web services like the old style ASP.Net web service (not new WCF) by adding a reference to the project using Add Web Reference option. In the pop up dialog you point it to the web service URL, Visual Studio.Net will then generate the required C#/VB.Net stub classes.

But its not to say that the web service will be at the same URL location expecially if you move between development - > QA -> production environments. How do you make the web service URL configurable?

The web service stub class Reference.cs (for C#) or Reference.vb (for VB.Net) has a URL property, you can always set this property in your code but its a nice way to rather do it when you call the constructor e.g.:

MyService service = new MyService(webserviceUrl);

You do this by adding the following constructor to the MyService class:

MyService(String theUrl)
{
Url = theUrl;
}

The downside (before Visual Studio.Net 2008) is that if you update the web reference (after adding an additional web method to the web service etc) the code get regenerated meaning your code will break and you will have to add the constructor again.

Fortunately Visual Studio.Net 2008 introduce partial classes which allow splitting definition of a class across multiple files.

The Reference.cs / Reference.vb class also is a partial class so you can add an additional partial class with the same name and same namespace:

public partial class MyService
{
MyService(String theUrl)
{
Url = theUrl;
}
}

Now you can update the web service reference without worrying about such problems.

So what is partial classes? Yes they allow you to split the definition of a class across multiple source files but why? In this sample it show that its for code generation mainly since you normally generate code then add your own additions (like a constructor in this case) but if you re-generate your custom code is lost. Partial classes (and later on I will introduce partial methods) resolve this problem. Why did MS add it? Look at LINQ to SQL which also generate code, now you can add custom logic like validation with partial methods without ever worrying that re-generation will break your logic.

Monday, March 23, 2009

Weekly Source Code: The Art Of Debugging In MS Visual Studio.Net

Bruno Terkaly wrote 11 lessons (more to come) on the art of debugging in MS Visual Studio.Net 2008. Very interesting and a must read to catch some tips! Did you know that MS Visual Studio.Net can debug Javascript? :-)

Visit his blog for the 11 lessons or this list that list the first 10 lessons.

Have fun!

Tuesday, January 27, 2009

Weekly Source Code: Strings Are Immutable

Happy New Year 2009! :)

The String type that is actually a class in both Java and C# are immutable meaning that once you create it, it can't get changed and manipulating it will create a new string object.

This is import for when you want to populate a string in a loop (like a for loop) since everytime it will create a new string that have a performance impact on your code.

For example:

String val = null;
for (int i = 0; i < 100; i++)
{
val += i;
val += ", ";
}

The code above will create a new string object everytime for 100 times, you might not see or notice the impact but it does have a performance impact (test it if you wish).

Its better to use a string builder/buffer class and then append the string values to it since the object is only created once.

For example:

StringBuilder val = new StringBuilder();
for (int i = 0; i < 100; i++)
{
val.Append(i);
val.Append(",");
}

The above C# code use the string builder without performance impact.

Note that in C# its called the StringBuilder class where in Java its the StringBuffer class both is similar in functionality.

Tuesday, October 21, 2008

C# From a Java Developer's Perspective

When I was moving from years of experience in Java to my first C# project with no knowledge of the framework or even the language I found this artical. It was very helpful to make the conversion from Java to the C# language especially since there's some differences that can bite you, when moving between them (like what I currently do on a daily basis).