Browse by Tags

All Tags » .Net Framework (RSS)

Use .NET Built-in Methods to Save Time and Headaches

During our everyday programming tasks we run into several repetitive code blocks that after the 20th time you implement them become really annoying. The worst case is to re-implement these code blocks every time, and the better case is to create a central class library with helper classes and methods. However, a large amount of these tasks can be achieved easily with built-in .NET methods. In this post I will go through several repetitive code blocks and show you how to implement them using built...
Posted by shayf | 3 comment(s)
תגים:, ,

Is String.IsNullOrEmpty Good or Bad?

I started to wonder about that when I was looking for an equivalent method in Ruby. Apparently, Ruby doesn’t come with such a method built-in, but you can add it very easily by using Ruyb’s monkey patching abilities. This is odd, because Ruby is the greatest language and has everything you possibly need (and IronRuby is even better! :) ). So why isn’t there an IsNullOrEmpty-like method? Well, they might just didn’t think it was important enough. And there might be a different answer, maybe the decision...
Posted by shayf | with no comments

I'm Proud to Present - IronRuby Unleashed

Hi readers! I'll start with the announcement - I'm writing a book! it is called "IronRuby Unleashed" and it will be published by Sams Publishing who has brought the world the great Unleashed series. What is the Book About? Well, I bet you've guessed it already - the book is about IronRuby. With version 1.0 right around the corner, it is more than important to have some reference books so developers can get started in no time. The book is a ~500 page reference and guide book...
Posted by shayf | 4 comment(s)

IDCC Session Voting is Open!

If you haven't heard yet, IDCC session voting is now open! Go and place your votes: http://idcc.co.il/sessions If yo're intersted in hearing about IronRuby or IronRuby on Rails , vote for my sessions: IronRuby - The Development Booster Machine IronRuby V1.0 is just around the corner and it's a great time to get your hands dirty! In this session you will get familiar with the main concepts of IronRuby and see how this exciting new .Net language will help you boost your everyday work. From...

A Handy Extension Method: Retrieving Session Variables

[More handy extension methods: raise events safely , get the current page load mode ] Retrieving session variables is a daily task for web developers. In order to fetch a session variable, we should check if it exists and only then convert it to the expected type and use it. For example: int myVal; if (Session[ "MyVar" ] != null ) { int .TryParse(Session[ "MyVar" ], out myVal); } // ... more code ... This is an annoying practice which can be easily replaced by extension methods...

A Handy Extension Method: Raise Events Safely

Raising events is a very common practice. We do that a lot and it’s pretty irritating to check for nullity every time: if (eventHandler != null ) { eventHandler(sender, e); } Here comes extensions methods to our aid again. With only two extension methods, we can cover all possible event handlers and raise our events safely in one line. For example: MyEvent.RaiseSafe( this , EventArgs .Empty); Or MyEvent.RaiseSafe<MyEventArgs>( this , new MyEventArgs(someParameter)); In order to achieve that...

C# Tip: Converting Whole Lists in a Single Line of Code

Lately I wanted to convert a list of items from one type to another. There is the straight way of doing so: writing a loop that iterates through the list, converts each item and generates the output list. There is a shorter way though. List has a ConvertAll method. You give it the output type and a delegate that converts a single item and that’s it. Writing this delegate yourself is just annoying… This is where the Convert class comes into action – you can use the Convert.ToXXX as the conversion...
Posted by shayf | 3 comment(s)

Make Your Application Extendable Using the DLR

It’s very common for applications to have a way to extend them. Extensibility comes in various ways and have multiple names too – plug-ins, add-ins, addons, etc. It seems, though, that one kind of extensibility was left to the very few – application macros. The concept is very simple – You don’t need to create a special Dll, implement a specific interface and register it somehow, you don’t even have to install an IDE. All you have to do is to open the relevant window, write some code and voila –...

.Net Tip: Console.Out

Shabat Shalom everyone! The Problem Sometimes you want your output to get written to the console and all you have is a method that receives a stream to output to. For example, DataSet.WriteXml gets different types of streams or a file path to write to... The Solution If you want to output the result to the screen, use Console.Out as your stream: DataSet d = GetDataSetFromSomewhere(); d.WriteXml( Console .Out); That's it! All the best, Shay.
Posted by shayf | with no comments

.Net Tip: Get and Set The User's Keyboard Layouts

Have you ever wanted to know what keyboard layouts are installed on your user's host? Have you ever wanted to set the keyboard layout in your form? .Net makes it easy for you! How do you do that? here it is: Get the installed layouts: foreach ( InputLanguage layout in InputLanguage .InstalledInputLanguages) { Console .WriteLine(layout.LayoutName); } Set the layout // This will set the keyboard layout to Hebrew InputLanguage .CurrentInputLanguage = InputLanguage .FromCulture( new System.Globalization...
Posted by shayf | with no comments

Why IronRuby is AWESOME!

Yesterday I had to detach a folder from the SVN supervision. In order to do that, one needs to delete the .svn folders within the folder and its subfolders. I decided to take advantage of IronRuby for that matter. The problem I ran into with this one was that the files on the .svn folder were read-only. This means that in order to delete the folder, I first have to loop over all the files and remove their read-only attribute. I searched the net for a way to do that without such a loop and found a...

The GetHashCode Method

I'm currently learning for a test I have this week in Data Structures course. I was reading about Hash tables when I realized - .Net has this "GetHashCode" method that I had always ignored, maybe I can learn for my test and C# on the same time! I immediately opened VS and created the following class: public class Student { public string Name { get ; set ; } public override int GetHashCode() { return 1; } } As you can see, the GetHashCode will return the same value for every instance...
Posted by shayf | 4 comment(s)

.Net Tip: Convert a String to Title Case

If you have a string like "hello world" and you want to use it in a title, you'll probably need to convert this string to "Hello World". The .Net framework gives us this ability out-of-the-box using the TextInfo.ToTitleCase method: string helloWorld = "hello world" ; Console .WriteLine(System.Threading. Thread .CurrentThread.CurrentCulture.TextInfo.ToTitleCase(helloWorld)); This will end up printing "Hello World" on the screen. Enjoy, Shay.
Posted by shayf | 2 comment(s)

Workaround: WebBrowser Control - Links Do Not Work

Yesterday I had to fix a bug that had been discovered at our form that used the WebBrowser control. Here is the description of the problem and solution. I hope it will save you the time I've spent on this. The problem When you use the WebBrowser.DocumentText in order to set the HTML of the document, some links don't work when you click on them. For example, the following link to the user's desktop doesn't work: <a href="file:///C:/Documents%20and%20Settings/user/Desktop/jon...

Extension Methods + Attributes = A Whole New World!

Earlier this week, a friend of mine (read his great blog here ) had tried to explain me why attributes were useful. We ended up with an extension method that reads attributes of enum values. Today I have the time to really think about that and I've just realized - this has tons of potential! Consider the following scenario - you have the following class: public string GetCityName( long userID) { // // Get data from some datasource... // return "Ney York City" ; } And now you're...
More Posts Next page »