{love to code?}

November 4, 2009

Invoking root directorie’s makefile from subdirectory in EMACS

Filed under: Linux — navaneethkn @ 12:27 AM
Tags: ,

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!

October 16, 2009

How overriding Object.ToString() helps

Filed under: .NET — navaneethkn @ 7:37 AM
Tags: ,

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. (more…)

September 28, 2009

Getting rendered HTML of a server control

Filed under: ASP.NET — navaneethkn @ 10:31 PM
Tags:

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))
    {
        control.RenderControl(writer);
    }
    return builder.ToString();
}

September 15, 2009

Disabling a custom control from Visual Studio’s toolbox

Filed under: .NET — navaneethkn @ 7:59 AM
Tags: ,

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

[System.ComponentModel.ToolboxItem(false)]
public class MyCustomControl : Label
{
}

MyCustomControl will not be displayed on the toolbox.

July 21, 2009

Single click builds for different environments using Visual Studio

Filed under: C# — navaneethkn @ 8:39 AM
Tags: , , ,

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? (more…)

July 5, 2009

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

Filed under: C++/CLI — navaneethkn @ 4:46 PM
Tags: , ,

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, (more…)

July 3, 2009

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

Filed under: C++/CLI — navaneethkn @ 4:15 AM
Tags: , ,

.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. (more…)

September 19, 2007

Writing a long string in multiple lines – c#

Filed under: C# — navaneethkn @ 1:41 PM
Tags: ,

Writing a big string will always produce scrollbars in the editor. So it’s better to split it to multiple lines which is more easy to read. This post discusses the various methods that can be used to split big lines into multiple lines.

Check this big line.

string BigLine = "bla bla  bla bla  bla bla  bla bla
bla bla  bla bla  bla bla  bla bla ";

Writing a string as shown above will produce error. This can be rewritten like

string BigLine = @"bla bla  bla bla  bla bla  bla bla
bla bla  bla bla  bla bla  bla bla ";

Or

string</font> BigLine = "bla bla  bla bla  bla bla  bla bla " +
"bla bla  bla bla  bla bla  bla bla ";

I always prefer the second one. Because first one will not move the next line to the starting of first line. And more over it won’t be possible to write first method for the following type of string.

string BigLine = @"bla bla  bla bla  bla bla  bla bla
bla bla  bla bla  bla bla\"test\"  bla bla ";

In this I am escaping " by specifying \. But .NET editor will produce an error.

Blog at WordPress.com.