Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Generate Classes From a Document | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades


Wipro Technologies
Tata Consultancy Services
Cognizant Technologies

Home » Articles » Generate Classes From a Document

Generate Classes From a Document

Article Posted On Date : Monday, January 25, 2010

Generate Classes From a Document

How can I create code from an existing table in a document.  For example, I have numerous input descriptions in Word Tables.  This article shows you how to use the table data to create input classes automatically.

Recently, I needed to create approximately 40  input classes from tables in a Word document.  The input file to the client application had about 40 different formats for the various types of input data lines.  Obviously, I could have created the classes  by hand, but that would have been very time consuming and error prone.  Since I had done something like this before I quickly determined to create a Macro in Visual Studio to do the code generation for me.  All of the classes will be alike in that they will be self populating data classes.  Their only method of note will be the Parse method, which will be called by the constructor.  The Parse method will parse the input line using a GetField method, also generated in the class, to pull the data from the input data line.  The input data line is passed to the constructor of the object when it is called by the client application.

Since I have developed the application in C#, I will generate the code for the classes in C#.  However, you will notice that macro is coded in VB.NET because that's the code in which Visual Studio Macros must be developed.  If you are a VB.NET developer, it would be a fairly simple job to change the code being generated to VB.NET.  If you do not understand C# that well, you can create the C# classes and then use a converter to convert the C# code to VB.NET code.

There are two ways that the macro can get the name of the class to be created.  First, you can create a class in your IDE and select all of the code, which would only be the code shown below.
Figure 1 - Class Definition Selected

Empty class

If you select all of the code shown above when the macro is invoked, then the macro will automatically pick up the name "NameLine" from the selection.  If you do not do this, and just place the cursor in a a code window where you want the new class to be inserted, the macro will display an input box for you to enter the name of the new class.

Now, I will show you an example input document with two tables describing two input lines.  For my application, I had approximately 40 tables describing 40 different input line types.  You will notice that I have selected the just the first two data columns in the table and copied them to the Clipboard.  The macro will retrieve the data and use it to generate the code.

Figure 2 - Table Definition of Input Line Format

Table Definition

Having selected the class definition shown in Figure 1, I will double click on the desired macro in the Macro Explorer to invoke the macro, which generates the code shown below in Figure 3.

Figure 3 - Input Class Generated by the Macro

   public class NameLine
      #region Class Constructor
// calling the constructor will populate the object
      public NameLine(string line)
      #endregion // constructor

      #region private methods
private void Parse(string line)
         RecordId = GetField(line, 1, 3);
         RecordType = GetField(line, 4, 6);
         LastName = GetField(line, 10, 25);
         MiddleInitial = GetField(line, 35, 15);
         LastName = GetField(line, 50, 25);

private string GetField(string dataLine, int stChar, int len)
if(dataLine.Length < 74) dataLine += Space(74 - dataLine.Length);
return dataLine.Substring(stChar - 1, len).Trim();

// Replacement for VB.NET Space Function
      public string Space(int spaceCount)
return String.Empty.PadLeft(spaceCount);
// method: Space
      #endregion // private methods

      #region public properties
public string RecordId {get; set; }
public string RecordType {get; set; }
public string LastName {get; set; }
public string MiddleInitial {get; set; }
public string LastName {get; set; }
      #endregion // public properties


Note that there is a Space function created in the class, which will be repeated in each class.  Although it is only a one line function, if you have a file of utilities, you could remove its generation from the macro and place a single copy of it in your utility file.  The space function is needed to ensure that the input line is at least as long as the data line definition, otherwise the GetField function will throw an exception because the SubString method will fail.  Also note that the GetField function must be generated in each class because it it generated with variables in it that match the length of the data definition being processed.

Next, comes the Macro code itself.  You will see that it calls a GetClipboard method that is described in another article on this site.  Click here to see that article.  Obviously, I could have used the CodeDom to create the code, but for a quick and dirty solution for creating code, macros are much easier to write than writing code genertion using the CodeDom.  And...when the macro is written and debugged, the code will work without fail.

Figure 4 - The Code Generation Macro

    Public Sub CreateCSInputObjectFromClipboardTwoInputFields()
Dim ts As TextSelection = DTE.ActiveDocument.Selection
Dim s2 As String = ts.Text
Dim mName As Match = _
Dim name As String = String.Empty

' get name from code window if extant
        If mName.Success Then
            name = mName.Groups("name").Value
            name = InputBox("Enter name for new Input Object", _
"Enter Object Name", "")
End If

        If String.IsNullOrEmpty(name) Then Exit Sub

        Dim s As String = GetClipboardData()
If s Is Nothing OrElse s.Trim.Length = 0 Then
            MsgBox("Nothing retrieved from clipboard.")
Exit Sub
        End If

        ' remove extraneous characters
        s = s.Replace(" ", "")
        s = s.Replace(
"*", "")
        s = s.Replace(
"-", "_")
        s = s.Replace(
"/", "_")
        s = s.Replace(
".", "")

Dim mc As MatchCollection = _
"^(?<id>w*) (?<len>(ddd|dd|d))", _
Dim cnt As Integer = 0
Dim stPtr As Integer = 1
Dim sb As New Text.StringBuilder(5000)
Dim sbprop As New Text.StringBuilder(5000)
Dim sbClass As New Text.StringBuilder(5000)
Dim reservedCnt As Integer = 1

If name.Length > 0 Then
            sbClass.Append("   public class " & name & vbCrLf)
"   {" & vbCrLf)

"      #region Class Constructor" & vbCrLf)

' build constructor
            sb.Append("      // calling the constructor will populate the object" & _
"      public " & name & "(string line)" & vbCrLf)
"      {" & vbCrLf)
"         Parse(line);" & vbCrLf)
"      }" & vbCrLf)
"      #endregion // constructor" & vbCrLf & vbCrLf)

"      #region private methods" & vbCrLf)
"      private void Parse(string line)" & vbCrLf)
"      {" & vbCrLf)

"      #region public properties" & vbCrLf)

For Each m As Match In mc
Dim id As String = m.Groups("id").Value
If id.Equals("Reserved") Then
                    id &= reservedCnt.ToString
                    reservedCnt += 1
End If

                Dim len As Integer = CType(m.Groups("len").Value, Integer)
                cnt += len

' create the automatic property for each match
                sbprop.Append("      public string " & id & " {get; set; }" & vbCrLf)
"         " & id & " = GetField(line, " & _
                   stPtr.ToString &
", " & len & ");" & vbCrLf)
                stPtr += len

            sbprop.Append("      #endregion // public properties" & vbCrLf & vbCrLf)

"      }" & vbCrLf & vbCrLf) ' end of Parse method

            ' create the GetField method for this object
            Const gf1 As String = _
"      private string GetField(string dataLine, int stChar, int len)"
            Const gf2 As String = "      {"
            Dim gf22 As String = "         if(dataLine.Length < " & _
               (stPtr - 1).ToString &
") dataLine += Space(" & _
               (stPtr - 1).ToString &
" - dataLine.Length);"
            Const gf3 As String = _
"         return dataLine.Substring(stChar - 1, len).Trim();"
            Const gf4 As String = "      }"
            Const gf40 As String = " "
            Const gf41 As String = "      // Replacement for VB.NET Space Function"

            ' create the equivalent method for VB Space function
            Const gf5 As String = "      public string Space(int spaceCount)"
            Const gf6 As String = "      {"
            Const gf7 As String = "         return String.Empty.PadLeft(spaceCount);"
            Const gf11 As String = "      } // method: Space"

            sb.Append(gf1 & vbCrLf)
            sb.Append(gf2 & vbCrLf)
            sb.Append(gf22 & vbCrLf)
            sb.Append(gf3 & vbCrLf)
            sb.Append(gf4 & vbCrLf)
            sb.Append(gf40 & vbCrLf)
            sb.Append(gf41 & vbCrLf)
            sb.Append(gf5 & vbCrLf)
            sb.Append(gf6 & vbCrLf)
            sb.Append(gf7 & vbCrLf)
            sb.Append(gf11 & vbCrLf)
"      #endregion // private methods" & vbCrLf & vbCrLf)

' concatenate all of the string builders to create the new class
"   }" & vbCrLf)

' put the new class into the code window
End If
    End Sub

Macros and add-ins are awesome in their ability to generate code that can be counted on to work the first time you use it.  I hope this example sparks a new interest in their use by you from time to time.  I don't write complex macros like this every day, but when I have a need for them, they are invaluable.

Sponsored Ads

Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions


Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions

Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |

Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.