views:

218

answers:

5

R# 4.5 (answers to 5 are welcome)
VS2008 (answers to VS2010 are welcome)
C# (FWIW)

I'm using a constructor (the question applies for methods, too), and there's Intellisense:

Foo Constructor Intellisense, showing the parameter variable names

I don't yet have a value to specify for this first parameter, "firstName". Today, I type "firstName", then let the IDE create that variable for me (which I initialize to some value).

I understand that the IDE will create the variable for me. I want it to create the variable name for me.

I don't want to have to type "firstName". I like the variable name the parameter author chose, and I want to use that variable name in my (calling) code.

Is there a way to have these acceptable variable names re-generated for me (the calling code) automatically as I move, parameter by parameter, through this line of (calling) code?

A: 

You can make a code snippet that creates the variable and inserts it as parameters.

MSDN Reference on snippets

jalexiou
Thank you for the response. This would require a unique snippet per method I'm calling, which is not practical. If I'm mistaken, could you please provide an example or cite reading that more precisely targets my problem?
lance
A: 

I don't understand your scenario entirely but I'm assuming you want to inject a variable name from calling assembly into the called code. if so, you may want to look into System.CodeDom that lets you create class and its memebers during runtime beside plethora of other functionality it offers.

Thank you for the response. I need a solution while I'm writing the code, not while it's running.
lance
A: 

I am pretty sure you can do it with Resharper or CodeRush/Refactor.

ralf.w.
A: 

It sounds like to me that what your trying to do is get out of typing at all! To have the IDE put the code in you intend so you don't have to. A quite lofty goal - with the exception that you'd put us all out of work ;-(

All fun aside, what you're probably reaching for is a code gen tool such as the T4 Toolbox ( one of my new favorite toys). If you're looking for a tool that will auto-generate your code snippets as you type, that's a tall order. The nearest thing available would be Resharper.

Here is an example of a class constructor I generated from my customization of T4 Toolbox templates:

 public partial class EvaluationController : SmartController
    { 
        private readonly IEvaluationService _evaluationSvc;
        private readonly IEvaluationMapper _evaluationMapper;
        private readonly IEvaluationCriterionMapper _evaluationCriterionMapper;
        private readonly IParticipantEvaluationMapper _participantEvaluationMapper;

        public EvaluationController( IEvaluationRepository repository, IEvaluationService evaluationSvc, IEvaluationMapper evaluationMapper, IEvaluationCriterionMapper evaluationCriterion, IParticipantEvaluationMapper participantEvaluation)
        {//     : base(repository, evaluationMapper)
              _evaluationSvc =  evaluationSvc;
              _evaluationMapper = evaluationMapper;
             _evaluationCriterionMapper = evaluationCriterion;
             _participantEvaluationMapper = participantEvaluation;
        }

If that is what you're after, the place to start would be: http://t4toolbox.codeplex.com/

I've got an example project where I use customizations of the templates to spin up my business classes, various methods & repository layer.http://t4tarantino.codeplex.com/ There's an example of the level of complexity of output you can generate at http://geekswithblogs.net/JamesFleming/archive/2010/08/18/code-generation-with-t4-toolbox.aspx

HTH

James Fleming
+1  A: 

You may get close to what you are looking for with VS2010.

  • Type p.Foo(

This will open the description of the currently selected constructor, out of the list of all constructors. If you type a letter, or hit ctrl + space, intellisense auto completion will open.

A difference here between VS2008 and VS2010 is named parameters. In VS2010, your completion list will have entries for the named parameters firstName: and lastName:.

  • Type the first letter of the parameter name (what you are referring to as "the variable name the parameter author chose")

Intellisense should jump straight to that entry, and allow you to do completion the same way it usually does.

  • Type a space, enter, or comma

Intellisense it will insert the identifier used for the named parameter. It won't insert the colon (unless you type it), so you don't have to use the named parameter feature to accomplish your goal here. You can just take advantage of the fact that the text you are looking for is in your completion list.

How you get Visual Studio to actually generate the local variables (which, according to your question, it seems like you have already solved) baffles me, and would be up to you to take care of :) If you've got that second problem licked, I'd like to know how, too.

Merlyn Morgan-Graham
This solves my problem. R# will create the variable for me once it sees that I've used (w/o the colon) an identifier which doesn't exist, leaving me to provide only that variable's *value*. The idea behind my question is to have me dictate only what the IDE can't ascertain. Tool IDE "types" everything *it* knows (the name of the object I'm calling, the name of its parameters, etc), leaving me to provide only what only *I* know (the actual *value* I'm passing). The rest is ceremony/boilerplate.
lance