Wednesday, February 11, 2009

[VS] Collapse all projects in a solution + How to setup Macro

This is not about increasing productivity - it's about being awesome.

How many times have you been collapsing manually all the projects in your solution ranting about the fact there's no option to do that automagically? Loadza times is the answer - unless you only work on small projects (which is not a crime btw). 

Anyway - after swearing at the screen a number of times I decided it was about time to do something about it and I googled it.

I found loads of awesome VS Macros to do what I was looking for but I hadn't a clue about how to create a VS Macro so I thought I'd share the whole thing.

First thing you need to go to Visual Studio Tools --> Macros --> Macro Explorer. Once you got that right click on MyMacros and create a new module (I called it CollapseAll).

Now edit the new module (double-click on it) erase whatever is in there and paste this stuff into it (if you want you can even try to understand what it does - but it's optional):


'Awesome Macros ripped-off
'from http://www.milkcarton.com/blog/

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module CollapseAll

Public Sub CollapseTopLevel()
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()

Dim solutionWindow As EnvDTE.Window = DTE.ActiveWindow
solutionWindow.Visible = False
Dim solution As UIHierarchyItem = GetSolution()

CollapseHierarchy(solution.UIHierarchyItems, True, True)
solutionWindow.Visible = True
DTE.StatusBar.Clear()
DTE.StatusBar.Progress(False)
End Sub

Private Sub CollapseHierarchy(ByRef items As UIHierarchyItems, ByVal IsRoot As Boolean, ByVal OnlyCollapseRootLevel As Boolean)
For i As Int32 = 1 To items.Count
If IsRoot Then DTE.StatusBar.Progress(True, "Collapsing", i, items.Count)
If (items.Item(i).UIHierarchyItems.Count > 0 And Not OnlyCollapseRootLevel) Then
DTE.StatusBar.Text = "Collapsing " & items.Item(i).Name
CollapseHierarchy(items.Item(i).UIHierarchyItems, False, False)
End If
items.Item(i).UIHierarchyItems.Expanded = False
Next
End Sub

Private Function GetSolution() As UIHierarchyItem
Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)
Dim uih As UIHierarchy = win.Object
GetSolution = uih.UIHierarchyItems.Item(1)
End Function

End Module


Now, the hard work is done - You'll see your CollapseTopLevel as a child of the CollapseAll module in you Macro Explorer and you can run it double clicking on it but we all know that sucks so the cool thing to do now is to assign an hotkey chord/combination to our kick-ass macro.

To do this you need to go to Tools-->Options-->Environment-->Keyboard. Pick your macro from the listBox with all the default VS stuff (remember it will be there like MyMacros.CollapseAll.CollapseTopLevel) and then assign a hotkey combination or chord to it (I used CTRL+K, CTRL+Y since it's not used by VS) and save. 

That's all.

P.S. I'd really like to thank Dan, who wrote the snippets I brutally ripped-off. The original article is awesome and there's plenty of nitty-gritty cut and paste potential there for developers in need.

kick it on DotNetKicks.com

Monday, February 2, 2009

[C++] What's the difference between BSTR and _bstr_t?

I stumbled upon this recently while maintaining some old code where BSTR and _bstr_t were being used interchangeably. This was generating a nasty (to say the least) bug which you can read all about here.

The main thing to understand here is that BSTR is a dumb pointer while _bstr_t is a class that wraps BSTR. The difference between the two is pretty much the same between char* and std::string.

_bstr_t also works like a smart pointer, so it will free the allocated memory when the variable is destroyed or goes out of scope. Another thing to say is _bstr_t has reference counting, increased every time you pass the _bstr_t variable by value (avoiding unnecessary copy) and decrement when it is no longer in use. Whenever all references are destroyed, the allocated memory for the string is freed up.

More about _bstr_t here, more about BSTR here.

That's it folks - lousy as ever.

Thursday, January 22, 2009

[C#, C++] Cascading Constructors

In C# you can call a constructor of your class from another constructor of the same class to do something like this:

class Cake
{
private string name;
private int price;

public Cake(int price, string cakeName)
{
this.name = cakeName;
this.price = price;
//init stuff
}

public Cake(int price):this(price, "Cheesecake")
{
//init other stuff
}
}

Unfortunately there's no way to do this in good old C++, but there are two common ways for simulating a similar behavior:

1) Combine two (or more) constructors via default/optional parameters:

class Cake
{
private:
string name;
int price;

public:
//combines Cake() and Cake("whatever")
Cake(int price, string name = "Cheesecake Giovacchia")
{
this->name = name;
this->price = price;
}
}

2) Use an Init method to share common code:

class Cake
{
private:
int price;
string name;

init(int price, string name)
{
//do crazy common stuff with price and name
}

public:
Cake(int price)
{
this->init(price, "Cheesecake Giovacchia");
}

Cake(int price, string cakeName)
{
this->init(price, cakeName);
}

}

That's about it, I prefer the first solution (the second kinda sucks) but if anyone knows better I am all ears.

I am done sucking for today.

Tuesday, January 13, 2009

Ask a Google Engineer - He Won't Answer

Recently I noticed a very promising initiative on Google Moderator: Ask a Google Engineer.

Geeks are encouraged asking questions to Guido Van Rossum & Co. with no limitation - from "Do you use Google toilet paper" to the infamous interview question "How would you sort 1 million 32-bit integers in 2MB of RAM?".

Turns out most of the question (roughly 1500 in total so far) asked by people (roughly 5000 so far) are silly complaints about how limited is this or why it's not possible to do that in gmail.
 
The rest are pretty much questions studied to impress Mr.Van Rossum with very elaborated Python blabbering or people trying to understand how to find the answers for the questions they are voting. 

But really - where are the answers? There are none (almost). Seen the amount of crap they're getting they're sitting it out to enable questions to be filtered by people votes.

Not exactly a success so far I'd say (maybe also because Google moderator kind of sucks imho)!

Thursday, January 8, 2009

Speed-up Visual Studio Startup!

Who thought getting rid of the splash screen could speed up Visual Studio this much?
Add a /nosplash to the visual studio shortcut target and see for yourself. 

Now, the question would be WHY is it so fast without splash-screen (something dodgy going on there), but this really made my day!