A Couple of C# String and Date Helpers
In JavaScript you can extend the available methods of built-in objects like Strings and do stuff like this:
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/, '');};
Which then allows you to do this:
alert(document.forms[0].SomeField.value.trim());
Which I always prefer to the alternative which would look something like this:
alert(trim(document.forms[0].SomeField.value));
Dunno why. I just think it looks more professional. There are probably good reasons to do it this way as well. Say, like, it stops you accidentally making two trim methods which do two different things.
Until today I'd never realised you can do this in "real" programming languages too. Well, at least you can in C#.
Here's a string extension helper method to encrypt a password:
public static class StringExtensions { public static string Encrypt(this string password) { return BitConverter.ToString( SHA1Managed.Create().ComputeHash( Encoding.Default.GetBytes(password) )).Replace("-", ""); } }
The key here is that the class and method are both static and also that we're using the "this" keyword before the string argument we're passing to the method. This tells C# the argument can be passed to it by calling our new method on a string. Like so:
Console.WriteLine("my password".Encrypt()); Console.WriteLine(Request["MyPassword"].Encrypt());
Nice.
I'm now in the process of retro-fitting this approach to a couple of apps I've done recently.
It follows that you can do the same thing for other objects, such as DateTime. Here's a helper method to turn a date in to words -- such as "Three weeks ago":
public static class DateExtensions { public static string ToWords(this DateTime dateTime) { //Code to do this for real is on Google et al return "Three weeks ago"; } }
The beauty of doing this in C# is that Visual Studio's Intelli-sense picks up on these new extensions and adds them to type-ahead, as you can see below in this Razor-based MVC View:
Working with C# and Visual Studio really is a pleasure. It makes learning to be a real programmer after all these years so much easier.
Just starting to get into C# over here. It looks completely awesome to me after only a few days of training. Flexible like Javascript, but solidly OO like Java. Neat stuff for sure. Lots to learn.
Reply
"Lots to learn".
Indeed. I can't imagine I'll ever completely master C#. There's just too much to it. But that's something I like about it. The fact that there's always something new to learn. My coding career (with Domino) had reached (a long time ago!) the point where I had nothing left to learn. Man, was I bored. Switching to C# has given me a fresh outlook on it all.
I just hope I don't regret that the prime of my career was wasted on Domino. And that it's not too late (at 36) to become a real programmer.
Reply
Is it a bit weird to have favourite string functions? Well even if it is, one of mine is String.Format which can be used to do placeholders, followed by arguments to fill in those placeholders.
e.g. Console.WriteLine(String.Format("Today is {0}!", DateTime.Today.DayOfWeek.ToString()));
Reply
Not weird at all. We're all geeks here!
I love String.Format() too and am only scratching at the surface of what it can do.
It took me a while to get in to the habit of using .Format instead of just using:
"Today is "+DateDateTime.Today.DayOfWeek.ToString();
Taking what I wrote above you can of course write:
string.Format("It was {0} since you last logged in", user.LastLoginTime.ToWords());
As for favourite function - hmm, dunno what mine is...
Reply