Archive for the ‘Coding’ Category

Replace a node with a set of nodes

Sunday, July 13th, 2008

In the last days in a my program, I should substitute a single node with a set of Node. The DOM interface (in this program I’m using the Xerces library) doesn’t provides an useful method to replace a single Node to a set of Nodes.

For apply this piece of code you should have:

  • parent node: The parent of the node that you want substitute;
  • target node: The node that you want substitute;
  • newNodes nodes: A set of nodes that you want add to the place of target node;

The code is the follow:

   1:  Node previous = null;
   2:  for(int i = newNodes.size() -1 ; i >= 0 ; i--) {
   3:      Node current = newNodes.get(i);
   4:  
   5:      if(i == newNodes.size() -1) {
   6:          parent.replaceChild(current, target);
   7:      } else {
   8:          parent.insertBefore(current, previous);
   9:      }
  10:      previous = current;
  11:  }

Transform Asynchronous method to Synchronous

Sunday, June 22nd, 2008

In the last day, I have found problem to transform an asynchronous call to synchronous. It is not very big problem but this problem is repeated for a big number of methods and components. I think that a good programmer should avoid the copy/paste of code but write a reusable code, for this I write a class that implement a lock/unlock mechanism.
This class has three methods

  • Lock: Lock the object.
  • Unlock: Unlock the object.
  • Wait: Caller remains to wait the Unlock method is called.

The implementation is based over the ReaderWriterLock class, this class protect an enumeration that describe the status of class

(more…)

Visual Studio Posters

Saturday, May 24th, 2008

In ogni ufficio di programmatori che si rispetti se questi lavorano con VS non può mancare un immenso poster (da mettere tra il tuo PM e te ;-) ) delle combinazioni di tasti per il nostro strumento :-) .

Allora eccovi i link:

Visual C# 2008

Visual C# 2005

Visual C++ 2008

Visual C++ 2005

Le istruzioni dal sito della Microsoft sono semplici e chiare

Download and extract the PDF
Send to your favorite full color printer/copier
Hang on Wall
Code
Smile

PowerCommands 2008

Tuesday, May 20th, 2008

Sono state rilasciate per Visual Studio 2008 i PowerCommands 1.1. Scaricabili liberamente dall’indirizzo:

http://code.msdn.microsoft.com/PowerCommands

I tre comandi che più mi hanno ingolosito sono “Open Command Prompt”, “Edit Proejct file” e “Remove and Sorting using”.

Open Command Prompt

Un comando molto utile è “Open Command Prompt” il quale apre un prompt dei comandi con caricato le variabili di VS. Questo viene molto utile se devi registrare il componente o devi comunque sfruttare qualche tools da riga di comando per il tuo codice (esempio xsd.exe).

Edit project file

Ti offre la possibilità di editare direttamente in VS il file .csproj (se si è in C# o il relativo file di progetto per gli altri linguaggi). Questa non è un attività molto comune ma può capitare di dover aggiungere particolari configurazioni non accessibili tramite le Properties di progetto. Esempio modificare la cartella di destinazione del output intermedia, (valore di default “obj”). Altro caso è rimuovere tag come quelli relativi ai tools di versionamento, Scc e compagni per intenderci.

Remove and Sorting Usings

Be non è odioso avere degli using inutili nel codice? In Java ed eclipse gli import inutilizzati sono segnati come warning, questo comando cerca di margina la situazione. :-)

IWizard and MultiProject

Wednesday, April 16th, 2008

Example: click here.

It is a Draft document In the previous month I should create a Wizard that it generates a solution with 3 complex projects. The first thinks is use the IWizard interface of Visual Studio. It works fine with a single project template but when you create a MultiProject template this mechanism doesn’t work! For exactly the replacement mechanism that is embedded in Visual Studio don’t propagates the repalcementsDictionary to the other template. I have resolved this problem in the follow way: I have added in each template a WizardExtenstion and a WizardData. In this way for each template is called my IWizard implementation and with static members a communicate between IWizard instances. In each call of RunStart I add in the repalcementsDictionary the correct value (for example projectname and all custom template parameters) for the current template

At this link you can download an example of solution click here.

Installing the example

In the example you can find a folder called “Template” and “IWizardImpl”, in the first folder you find the template that should be decompressed in <User>/Visual Studio 2008/Templates/ProjectTemplates/Visual C#/ (the same thing with VS2005 but the folder is Visual Studio 2005 not Visual Studio 2008) folder.

After that you must register in the GAC the IWizardImpl.dll assembly. This example came with a Debug version placed in IWizardImpl\bin\Debug folder.

You should call the “gacutil.exe” application from the Visual Studio command line or providing full path (for example” C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe”) with the /I param and passing the dll that you won’t store in the GAC.

gacutil.exe /i IWizardImpl.dll

For more detail you can found information about this steps on the MSDN about Visual Studio IWizard.

Example structure of template

The template folder contains a MutiProject template this template refers other two templates . Each templates call the same Wizard but each call has a different custom parameter.

This custom parameter is defined in the WizardData tag, an example is the follow portion of template file of UserControl/MyTemplate.vstemplate.

<WizardData Name="ProjectPart">UserControl</WizardData>

This tag at runtime means that in the replacementDictionary an element with key “ProjectPart” and value “UserControl” is added and you can read this value.

Summering the steps

  1. Understands if the calling template is a MultiProject or a Project with WizardRunKind enumeration
    1. If it is a MultiProject, shows my wizard and collect some information and stores it in statics members.
    2. If a Project I read the custom parameter to understand if I working over the Skins or UserControl. Go to step 2
  2. Populates the replacementDictionary with my customization and replacing for example the projectname with an appropriate value.

This steps are useful for avoiding the problem of no replacing mechanism don’t work properly in the case of MultiProject template.