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):
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.
public class CSharpClassGen:AClassGen
{
public CSharpClassGen()
{
}
#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.
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.