Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#
Article

Yet Another Class Generator

Rate me:
Please Sign up or sign in to vote.
3.09/5 (7 votes)
9 May 20062 min read 21.4K   646   24   3
An essential tool for every programmer, this tool will generate the setters/getters for classes, in different programing languages.

Sample Image

Introduction

This tool is very simple but saves a lot of time for the developer, especially for beginners, when you don't know how to start and you keep postponing it. This tool enables you to generate code for a class, you only need to enter the name of the different fields/members and select/type the nature of those fields and specify if they have getters and setters, and then generate the code. Whatever language you are using, C#, C++, or Java, you can download the demo file and use it.

Background

I am a C++ teacher, and in order to fix in the mind of my students the concepts of object oriented programming, classes, the getter and the setters, I asked them to write a program that generates code for a class when its different fields are known. And to impress them, I wrote this tool that generates a class for different programming languages (C#, C++, Java), with a color syntax editor.

The code

This a class generated by the beta version of the tool, and is the base of this tool. It represents a field (name, nature of the field, default value in the constructor of the class, implements the setter or the getter):

C#
public class classFields
{
    private string m_fieldType=""; 
    private string m_fieldName=""; 
    private string m_defaultValue="";
    private bool m_inConstruct=false;
    private bool m_inSetter=false; 
    private bool m_inGetter=false; 

    public string FieldType 
    {
        get
        {
            return m_fieldType ;
        }
        set
        {
            m_fieldType = value ;
        }
    }
    public string FieldName 
    {
        get
        {
            return m_fieldName ;
        }
        set
        {
            m_fieldName = value ;
        }
    }
    .......

This class inherits from AClassGen and is used to generate a class in C#. AClassGen implements the text indentation for the generated code.

C#
public class CSharpClassGen:AClassGen
{
    public CSharpClassGen()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    #region IClassGen Members

    public override string genClass(System.Collections.ArrayList lst, 
                                    string className)
    {
        StringBuilder sb = new StringBuilder();
        int nbIndent=0;
        addLine(nbIndent,ref sb,"class " + className + " \n");
        addLine(nbIndent,ref sb,"{ \n");
        nbIndent++;
        for(int i=0;i<lst.Count;i++)
        {
            classFields obj = (classFields)lst[i];
            string eType=obj.FieldType;

            string eName =obj.FieldName;

            string dfV =obj.DefaultValue;

            addLine(nbIndent,ref sb,"private " + eType + " m_"+ eName );
            if(dfV.Trim().Length>0)
            {
                if(eType.Trim().CompareTo("string")==0)
                    sb.Append(" = \"" + dfV.Trim()+ "\" ");
                else
                    sb.Append(" = " + dfV.Trim());
            }
            sb.Append("; \n");
        }

        for(int i=0;i<lst.Count;i++)
        {
            classFields obj = (classFields)lst[i];
            string eType=obj.FieldType;

            string eName =obj.FieldName;
            bool inSetters = obj.InSetter;
            bool inGetters=obj.InGetter;
                
            try
            {

                if(inSetters ||inGetters)
                {
                    addLine(nbIndent,ref sb,"public " + eType + 
                       " "+ eName.Substring(0,1).ToUpper() + 
                       eName.Substring(1) + " \n" );
                    addLine(nbIndent,ref sb,"{\n");
                    nbIndent++;

                    if(inGetters)
                    {
                        addLine(nbIndent,ref sb,"get\n");
                        addLine(nbIndent,ref sb,"{\n");
                        nbIndent++;
                        addLine(nbIndent,ref sb,"return m_" + eName+" ;\n");
                        nbIndent--;
                        addLine(nbIndent,ref sb,"}\n");
                    }

                    if(inSetters)
                    {
                        addLine(nbIndent,ref sb,"set\n");
                        addLine(nbIndent,ref sb,"{\n");
                        nbIndent++;
                        addLine(nbIndent,ref sb,"m_" + eName+" = value ;\n");
                        nbIndent--;
                        addLine(nbIndent,ref sb,"}\n");
                    }

                    nbIndent--;
                    addLine(nbIndent,ref sb,"}\n");

                }
            }
            catch(Exception ex){}
        }
        nbIndent--;
        addLine(nbIndent,ref sb,"} \n");

        return sb.ToString();
    }
}

Points of Interest

I used DevAge Sourcegrid 3.0 for developing this application, it is a very flexible grid. I modified Michael Gold's tool: "Simple Color Syntax Code Editor for PHP written in C# and .NET", and implemented it for the syntax coloring.

ToDo:

  • Add VB.NET to the list of supported programming languages.
  • Integrate this tool into Visual Studio as an Add-In.
  • Test the code generated in Java, C++.
  • Generate the constructor of the class.

Conclusion

This is my first article, so please go easy on me. Hope that you will use it. And of course, if you are using this to generate code from an UML chart, you will not find it useful.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Lebanon Lebanon
HB is mean (opp. nice), he lives on a planet called earth, and he writes programms since the XX century. He doesn't work in a company (but owns a company) and he is not an MVP.


Comments and Discussions

 
GeneralThanks for the update, more suggestions please [modified] Pin
Alexandru Matei21-Oct-06 22:25
Alexandru Matei21-Oct-06 22:25 
GeneralVery nice [modified] Pin
Alexandru Matei1-Aug-06 1:57
Alexandru Matei1-Aug-06 1:57 
GeneralThank You Pin
defwebserver9-May-06 14:43
defwebserver9-May-06 14:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.