Archive of: Programming

  1. Using Extension Methods in .NET posted September 3, 2010

    Extension methods are a handy way of adding your own custom methods to a class when you normally wouldn’t be able to. .NET treats extension methods exactly like instance methods. In fact, in your day-to-day .NET programming you may be calling extension methods and not even know it.

    Let’s say we have access to an API with a simple Person class. Also, for the purposes of this post, let’s say we don’t have access to extend or modify this class.

    public class Person {
    
      public Person(string first, string middle, string last) {
        FirstName = first;
        MiddleName = middle;
        LastName = last;
      }
    
      public string FirstName { get; set; }
      public string MiddleName { get; set; }
      public string LastName { get; set; }
    }
    

    In order to get the full name of a Person we’d have to do something like this:

    Person p = new Person("Robert", "N", "Greiner");
    string fullName = String.Format("{0} {1}", p.FirstName, p.LastName);
    

    This is perfectly fine, but what if you have to do this in several places? You’d either have to copy/paste this code wherever you needed it (bad idea, what if your boss came in later and said that she… Read the rest

  2. Grouping Radio Buttons in ASP .NET MVC 2 posted August 27, 2010

    Here’s a simple issue I ran into today when dealing with radio buttons in an ASP .NET MVC 2 application.

    Radio buttons should only allow the user to select a single option out of multiple choices. This should be enforced by only allowing one radio button to be selected for each option set. But, what if your application has multiple options that all need radio buttons? How do you distinguish the radio buttons from one option from the other.

    This is where groups comes in. By setting the GroupName property for each of your radio buttons, you can specify which buttons to associate to an option.

    <div>
      <asp:RadioButton ID="ActionMatch" GroupName="RegexActions" Checked="true" Text="Match" runat="server" />
      <asp:RadioButton ID="ActionReplace" GroupName="RegexActions" Text="Replace" runat="server" />
    </div>
    

    Also, note that in ASP .NET MVC 2 there is no default group, so you must define a GroupName in order for the radio buttons to work properly.

  3. Using Stopwatches and Timers in .NET posted June 2, 2010

    Two problems that plague new programmers and seasoned professionals alike is the need to delay program execution and get an accurate time how long something takes to execute. Luckily, there are probably a few thousand ways to solve each problem. Unfortunately, most of them center around laziness (the bad kind) and unreliability. In this article, I’ll attempt to show you how to implement the different Timer and Stopwatch classes in .NET to help ensure your application will work as intended for years to come. Because, let’s face it, you probably aren’t going to be re-factoring any time soon.

    Timers

    First, we’ll start of with an introduction to Timers and how not to implement them. Timers allow you to control the execution of your application by being able to define when an action takes place.

    I remember in my very first high-school programming class, we were instructed to move a circle from one end of the screen to the other. The problem every student eventually encountered was, the circle had already moved to the edge of the screen by the time the graphics got loaded and we didn’t get to see any animation. This happened because the function that incremented… Read the rest

  4. Iterating Through a Dictionary in C# posted May 23, 2010

    A Dictionary allows you to store items in a collection using a key/value paring. By storing your data this way, you get all of the functionality of a standard .NET Collection with the added benefits of Hashing.

    I’m going to start off by showing you how to get data into a Dictionary. Then, I’ll move on to how to access specific data entries. And, finally, I’ll demonstrate two methods of iterating through the Dictionary. This should give you enough information to get started with Dictionaries if you’ve never seen them before, and hopefully, teach you something new if you are already familiar with them.

    Inserting Into a Dictionary

    First, let’s get some data into our Dictionary by adding some of the hard workers of Dunder Mifflin.

    Dictionary<int, Person> employees = new Dictionary<int, Person>();
    employees.Add(1000, new Person("Jim Halpert"));
    employees.Add(1001, new Person("Pam Halpert"));
    employees.Add(1002, new Person("Andy Bernard"));
    employees.Add(1003, new Person("Dwight Schrute"));
    employees.Add(1004, new Person("Michael Scott"));
    

    Retrieving Data From a Dictionary

    Now that we have our employees in memory, we can access them in near constant time (O(1)) by using their ID.

    Person p = employees[1003];  //Select Dwight
    

    Iterating Through a DictionaryRead the rest

  5. Expressing Many-to-Many Relationships in Hibernate posted May 2, 2010

    As humans, we tend to think of the world in objects. For instance, my car has four tires and my body has a pair of arms and legs. Since everything around us is thought of as objects, then why would we want to write code that stores data relationally? The short answer is; we are forced to based on the way traditional databases store data.

    Let’s say for instance we have a group of Students that wish to enroll in several University Courses. Storing the Students and Courses in a relational database is trivial, however, how do we express Students that are enrolled in Courses? This is a typical many-to-many relationship where any number of students can be enrolled in any number of courses.

    In the world of databases, a single table is created for Students and another for Courses. Then, a third table is created that holds the students that are enrolled in each course. The Enrolled table is called a cross-reference table.

    This poses a problem for object oriented languages such as Java, C#, and Ruby because, while we don’t mind creating separate Student and Course objects, creating a third Enrolled object is poor OO design and will… Read the rest

.NET Algorithmic Art ASP ASP .NET ASP .NET MVC audio Bill Gates C# code review Computational Art CouchDB CSS Database DateTime debug DevDays eclipse ethics FileSystem Git Improvement iPhone Java JQuery Knopflerfish Layouts mail Microsoft Oracle OSGi Processing Python Ruby SMTP SOA Software Piracy Source Control SQL StackOverflow tools TortoiseGit training Web Standards Windows XHTML