Share |

Start using AutoCode

This article shows the basis of using AutoCode. In this walkthrough we will generate a class with some properties to familiarize with AutoCode inline code generation.

Create a new Console Application project

Point the cursor just at the end of the class, like in the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    | <---- Set the cursor here
}
Invoke AutoCode Command Input Dialog

Press Control+Enter and the command input dialog appears. Write in “int id string name Person class”.


dlgclass[16]

The class Person will be generated with the properties Id and Name:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    public class Person
    {
        private int _id;
        private string _name;

        public Person()
        {
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        |
    }

}

Invoke AutoCode inline

Commands can also be invoked directly from the editor.

Write “string LastName p” just after the last property, like in the figure, and press Ctrl+Enter.

        }
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        string lastName p


This will execute the Property command generating a get/set property.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    public class Person
    {
        private int _id;
        private string _name;
        private string _lastName;

        public Person()
        {
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
    }

}

Note that the command generated also the field _lastName at the top of the class.

Command PropertyAutomatic (pa)

Let’s add some more properties to explore the different signatures we can generate.

This command will generate an automatic property. For example:

string phone pa

public string Phone { get; set; }

 

Command PropertyIf (pif)

This command will generate a property that will instantiate a value in case it is null. For example:

Address HomeAddress pif

        public Address HomeAddress
        {
            get
            {
                if (_homeAddress == null)
                {
                    _homeAddress = CreateHomeAddress();
                }
                return _homeAddress;
            }
        }

 

Overriding ToString method

Now lets override the ToString method to return a formatted representation of the class properties.

Set the cursor at the end inside of the class and execute the tostr command.

dlgtostr[5]

This will override the ToString() method in our class.

    public class Person
    {
        private int _id;
        private string _name;
        private string _lastName;

        public Person()
        {
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        override public string ToString()
        {
            string str = String.Empty;
            str = String.Concat(str, "Id = ", Id, "\r\n");
            str = String.Concat(str, "Name = ", Name, "\r\n");
            str = String.Concat(str, "LastName = ", LastName, "\r\n");
            return str;
        }
    }

 

Instantiating the Person class

Lets now use another command to instantiate the class with default values.

Inside the main method we write:

Person ci

    class Program
    {
        static void Main(string[] args)
        {
            Person ci
        }
    }

 

Then we press Ctrl+Enter and we get the following:

    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.Id = 9999;
            person.Name = "Name";
            person.LastName = "LastName";     
        }
    }

 

Note that the properties are instantiate with default values, and only the public properties are instantiated.

Also note that inherited properties are not instantiated with this command. If required, you can use the recursive version of this command: CreateInstanceRecursive (cir) 

Tips and Tricks

Some useful tips to finish with this introductory article.

  • Pressing TAB key inside the Command Dialog we can move over the last executed commands.
  • To display more information about a command, press the help icon that appears at the right of the Dialog
  • If you want to see what’s inside any of the AutoCode Command use the Open command. For example, execute “p open” and the templates of the Property command will open in Visual Studio.
Summary

In this article we show how to invoke commands using the Command Dialog as well as directly from the editor.

We also show some of the existing commands to generate inline code and create classes, properties and other pieces of commonly used code.

Using the Input Dialog you can explore and try other useful commands. Some of them will generate code while some other will execute another useful task.

 


Comments

Re: Start using AutoCode

posted by Mark on Wednesday, February 09, 2011

Nice! Waiting for more useful tips.

Re: Start using AutoCode

posted by Fran on Wednesday, February 09, 2011

Waiting is well deserved!! Cool new version and website, keep improving!! P.S. Add a captcha to the comment form to avoid spam P.S. Do not forget your passport just in case you travel "abroad"

Re: Start using AutoCode

posted by oxy96 on Tuesday, March 01, 2011

Very nice tool :) Class generation command can be more usefully, if it will create new file and add it to project :) Implementation isn't hard. I already have this functionality. I want write more, but i'm thinking about some documentation or something... Thanks again! I will follow this blog for more info, tips and ideas.

Re: Start using AutoCode

posted by Mark J. on Thursday, March 17, 2011

Just GREAT TOOL. Keep it simple and go on.

 


Add a comment