.NET
  Home arrow .NET arrow Page 17 - Outputting Code
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Outputting Code
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2004-10-04

    Table of Contents:
  • Outputting Code
  • Understanding Script Directives
  • Understanding Other Features of the Generation Harness
  • Extending the Harness
  • Examining Code Generation Mechanics
  • Exploring Details of XSLT Code Generation
  • Creating Named Templates
  • Creating Match Templates
  • Supporting Stylesheets
  • Exploring Details of Brute-Force Generation
  • Creating a Class
  • Creating the Support Template
  • Understanding Types
  • Building a CodeDOM Graph
  • Building the Code Structure
  • Outputting Assignments
  • Creating Arrays
  • Exploring Other Features
  • Working with the CodeDOM

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Outputting Code - Creating Arrays


    (Page 17 of 19 )

    You create arrays that can be assigned to previously declared array variables using the CodeArrayCreateExpression:

    entry.Statements.Add(New CodeAssignStatement( _
    New CodeVariableReferenceExpression("a2Ints"), _
    New CodeArrayCreateExpression("System.Int32", 10)))
    Dim varAInts As New CodeVariableReferenceExpression("aInts")
    entry.Statements.Add(New CodeAssignStatement( _
    varAInts, _
    New CodeArrayCreateExpression("System.Int32", _
    New CodePrimitiveExpression(0), _
    New CodePrimitiveExpression(1), _
    ' Additional assignments snipped
    New CodePrimitiveExpression(9))))

    Assigning Array Values

    Accessing individual values within the array requires a CodeArrayIndexerExpression. This creates output such as aInts(iValue) in VB .NET or aInts[iValue] in C#. The next example assigns the fourth (see footnote 12) element in the array to the previously declared variable named iValue:

    entry.Statements.Add( _
    New CodeAssignStatement( _
    New CodeVariableReferenceExpression("iValue"), _
    New CodeArrayIndexerExpression( _
    varAInts, New CodePrimitiveExpression(3))))

    Footnote 12. .NET arrays are zero based.

    Okay, enough simple stuff. Let’s look at something a little more complex. What would it take to output the following code?

    Dim i as Integer
    aInts(i) = aInts(i + 1)

    After declaring the variable i, create an expression variable pointing to i. This and the previously created expression variable pointing to the array save some typing:

    entry.Statements.Add( _
    New CodeVariableDeclarationStatement( _
    "System.Int32", "i", _
    New CodePrimitiveExpression(0)))
    Dim varI As New CodeVariableReferenceExpression("i")

    Outputting the target code requires array indexers for both the left and right operands of the assign statement. The left operand uses the variable reference expression pointing to the array and the variable reference expression pointing to i. The right operand also uses the variable reference expression for the array. For the array index, it uses a binary operator expression and the addition operator. The left operand of this binary operator expression is the variable reference expression for i, and the right is a primitive expression for 1:

    entry.Statements.Add( _
    New CodeAssignStatement( _
    New CodeArrayIndexerExpression( _
    varAInts, varI), _
    New CodeArrayIndexerExpression( _
    varAInts, _
    New CodeBinaryOperatorExpression( _
    varI, CodeBinaryOperatorType.Add, _
    New CodePrimitiveExpression(1)))))
     

    This doesn’t output a particularly complex line of code. It outputs a short, rather ordinary line of code, and the CodeDOM template is beginning to feel convoluted. If you understand how this piece of code works, though, you’ll be able to extend it to more complex code statements on your own. 

    This chapter is from Code Generation in Microsoft .NET by Kathleen Dollard (Apress, 2004, ISBN: 1590591372). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Apress Publishing


     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek