Invoking root directorie’s makefile from subdirectory in EMACS

To compile, M-x compile is used in emacs. The value displayed in the mini-buffer for this command is stored in the compile-command variable. When you execute, make -k, emacs executes the command in the directory on which the currently opened file belongs. This won’t help as you may not have makefile in the current directory.

Most of the projects will have only one makefile which will be available in the root directory. To invoke this makefile from a subdirectory, you can use -f switch and specify the makefile path. Something like:

make -f project_root_directory/Makefile

Still, emacs executes this command from the current directory. This method has got potential problems like relative paths used inside the makefile won’t work. A reliable method would be to ask emacs to change the current directory to project’s root, execute the makefile and come back to the current directory. Fortunately, emacs supports cd command which can be used to change the directory and cd – to come back to last directory. After issuing the compile command (M-x compile), you can type

(cd /project_root_directory && make && cd -)

Using this method, emacs executes the makefile from the root directory. You can add the following to .emacs file if you need this trick always.

(setq compile-command "(cd /project_root_directory && make && cd -)")

Happy programming!

How overriding Object.ToString() helps

System.Object provides a ToString() method which gives a human readable string about the object. Many people haven’t realized how useful is this tiny method.

Consider the following code:

namespace PersonDetails
    class Person
        public Person(string firstName, string lastName, int age)
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

    class Program
        static void Main(string[] args)
            Person chuckNorris = new Person("Chuck", "Norris", 20);
            Console.WriteLine(chuckNorris.ToString()); // Prints PersonDetails.Person

The information printed is not at all helpful. Let us override ToString() and see the difference. Continue reading

Getting rendered HTML of a server control

This post shows a generic method that can return the HTML string generated by ASP.NET server control after the rendering.

string GetRenderedOutput<T>(T control) where T : Control
    StringBuilder builder = new StringBuilder();
    using (StringWriter sWriter = new StringWriter(builder))
    using (Html32TextWriter writer = new Html32TextWriter(sWriter))
    return builder.ToString();

Disabling a custom control from Visual Studio’s toolbox

When a custom control is created, Visual Studio will show it in the toolbox for dragging and dropping. This can be annoying when you create a composite control which is a combination of several other custom controls where VS will display all custom controls in the toolbox. System.ComponentModel.ToolBoxItemAttribute can be used with the controls that you want to hide from toolbox. Apply this attribute to all the controls that you want to hide from toolbox.

Sample code

public class MyCustomControl : Label

MyCustomControl will not be displayed on the toolbox.

Single click builds for different environments using Visual Studio

A neat build system is vital for all projects. Good build system should be capable to work without *any* user intervention. Here is what Joel Spolsky says on his post

Can you make a build in one step?

By this I mean: how many steps does it take to make a shipping build from the latest source snapshot? On good teams, there’s a single script you can run that does a full checkout from scratch, rebuilds every line of code, makes the EXEs, in all their various versions, languages, and #ifdef combinations, creates the installation package, and creates the final media — CDROM layout, download website, whatever.

Ask yourself the same question – Can you build your application in one step? Continue reading

Converting console application to windows application – C++/CLI

Have you ever needed to convert a console application to windows application?

Recently, I worked on an application which was created to run on console, but later decided to move to windows application. To make it a windows forms application, you only have to add new windows form to the project. VS will add all necessary references automatically.

Modify your main method like, Continue reading

Using application configuration file (app.config) with C++/CLI applications

.NET applications uses app.config file to keep the application configuration entries like database connection strings, logging information etc.  Using Visual Studio, you can add a new app.config file to your application. In C# and VB.NET projects, app.config will be copied automatically to the output directory and it will be named like <Application>.exe.config where <Application> is your executable name. ConfigurationManager is a helper class which provides a neat interface to the contents of app.config.

With the above information, let us write a simple application which reads AppSettings node from the application configuration file. Continue reading