| Subcribe via RSS

C# 4.0 – Optional Parameters

April 14th, 2010 | 1 Comment | Posted in Geek, Visual Studio

I know, I know, VB has had them for ages.

But I don’t care… Now C# has them too!

This will greatly reduce method overloads!
Check this bit of code (sorry it’s not a “real world” example)

public class TestClass
{
    public void DoSomething(string parameterOne, string parameterTwo, string parameterThree)
    {
        DoSomething(parameterOne, parameterTwo, parameterThree, null);
    }

    public void DoSomething(string parameterOne, string parameterTwo, string parameterThree, string parameterFour)
    {
        if (parameterFour != null)
        {
            //doing somthing with the parameters.
        }
    }
}

The DoSomething method has an overload on it that takes a fourth parameter (in this case parameterFour)

When we use the above code, our intellisense looks like this:

 

overload1

…and the second overload

 

overload2

As we can see, the method has 2 overloads. One taking parameterFour, one not.

New in C# 4.0, we can use something called optional parameters.

This allows us to change our method to:

public class TestClass
{
    public void DoSomething(string parameterOne, string parameterTwo, string parameterThree, string parameterFour = null)
    {
        if (parameterFour != null)
        {
            //doing something with the parameters.
        }
    }
}

As you can see, we’ve removed the overloaded method – I’m sure you can appreciate, if we had a method with many overloads, this results in much cleaner code :-)

Now, our intellisense looks like this:

intellisense-with-optional-parameter-method

In my opinion, this looks a bit confusing.

Sure, I’ll get used to it, but in my opinion, the method is still sort of overloaded… just with optional parameters!

VN:F [1.9.3_1094]
Rating: 3.0/10 (3 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Tags:

Location of Visual Studio Templates Directory

July 2nd, 2009 | 1 Comment | Posted in Visual Studio

The location of the Visual Studio Templates Directory (when installed on Vista) is as follows:

C:\Users\<user>\Documents\Visual Studio <version>\Templates

Replace <user> with your username
<version> with the version of VS (in my case its 2008) so my path is:

C:\Users\ABrown\Documents\Visual Studio 2008\Templates

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Tags:

Show line numbers in Visual Studio 2008

June 22nd, 2009 | No Comments | Posted in Visual Studio

I recently reset my settings in Visual Studio which, irritatingly, made my line numbers disappear.

To re-instate the line numbers, there is a simple process:

Click:
Tools > Options > Text Editor > All Languages > General

On the right, under Display section, check Line numbers (see below)

Click Ok.

showLineNumbersVS2008_1

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Tags:

Reset IDE settings in Visual Studio 2008

June 22nd, 2009 | 5 Comments | Posted in Visual Studio

I recently acquired a PC at my new job where there was an existing installation of Visual Studio.

This was fine, however it was set up for a VB.net developer.
Whilst I do use VB.net, I didn’t want my VS set up this way.

I wanted to re-instate that lovely screen you see when you fresh install VS (below)

resetVS2008Settings_0

To do this, simply click:

Tools > Import and Export Settings Editor

resetVS2008Settings_1

Click Reset all settings

resetVS2008Settings_2

I didn’t backup my settings as the guy working here before had gone… But this screen gives you the option to.

resetVS2008Settings_3 

And there we are.
All settings can be reset to a theme of your choice.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Tags: