Tags: , , , , , , , | Categories: Genel Posted by okutbay on 30.03.2010 10:55 | Yorumlar (5)

Bazen iki tarihin arasında geçen süreyi bulmanız gerekir. Bu kişinin şu anki yaşı olabileceği gibi bir işçinin o gün çalıştığı süre de olabilir. C# bize bu konuda yardımcı olmak için TimeSpan tipini sunar. Bu tipi kullanarak iki tarih arasında geçen süreyi farklı şekillerde alabiliriz. Örneğin iki tarih arasında kaç saat olduğunu bulmak istiyorsak TotalHours özelliğini kullanabiliriz. Eğer iki tarih arasında geçen sürenin sadece saat kısmı bizi ilgilendiriyorsa Hours özelliğini kullanabiliriz.

    1 DateTime myStartTime = Convert.ToDateTime("30.03.2010 08:04:00");

    2 DateTime myEndTime = Convert.ToDateTime("30.03.2010 18:02:00");

    3 TimeSpan myWorkingTime = myEndTime - myStartTime;

    4 double myWorkingHours = myWorkingTime.TotalHours;

Bu işi uygulamanız içinde birden çok kullanacaksanız bir method haline getirmek faydalı olacaktır.

    1 public static double GetWorkingHours(DateTime startTime, DateTime endTime)

    2 {

    3     TimeSpan workingTime = endTime - startTime;

    4     double workingHours = workingTime.TotalHours;

    5     return workingHours;

    6 }

 

bu yeni metodu soyle kullanirsiniz.

    1 DateTime myStartTime = Convert.ToDateTime("30.03.2010 08:04:00");

    2 DateTime myEndTime = Convert.ToDateTime("30.03.2010 18:02:00");

    3 double myWorkingHours = GetWorkingHours(myStartTime, myEndTime);

 

Not:Methodun ve kodun aynı classta olduğu varsayılmıştır...

Kolay gelsin...

Bu yazıyı ilk değerlendiren siz olun

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , | Categories: Tip, Programlama, Genel Posted by okutbay on 26.02.2010 11:41 | Yorumlar (0)

You can always write comment to your codes. But comment itself can be outdated and misleads a developer. So writing self-documented codes must our first aim to make our code more maintainable.

In my opinion a big step to achieve self-documented code begins writing if clauses.

If a developer understands if clause, he/she understand purpose of the code block more easily.

But how can we write more readable if clause.

Very easy: Don't write logical comparisons into if clause.

    1 if ((preprocessedFailedEmails.Count > 0) || (failedEmails.Count > 0))

    2 {

    3     //some code

    4 }

Although this is a very simple if clause it's a bit hard to get what is happening there. Let's make it more readable.

    1 bool hasErrors = ((preprocessedFailedEmails.Count > 0) || (failedEmails.Count > 0));

    2 if (hasErrors)

    3 {

    4     //some code

    5 }

Happy coding...

Bu yazıyı ilk değerlendiren siz olun

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5