Saturday, April 28, 2007
Code Creator - a C# based mini PHP
Creating code dyanamically is a very strong requirement for any advanced programming tasks. Code Creator source will be available in the near future. The software and data files presented at Demo Camp on Apr 30th can be found here. A PowerPoint presentation that is also available.
Also see Creating as SilverLight Control with CodeCreator for an example on using CodeCreator.
Code Creator supports the creation of almost any type of code including XML.
Code Creator competes very well with PHP in the .net environment simply because it leverages .net's power. Above all Code Creator is small and simple.
An example of CodeCreator creating C# code segment.
-Create typed classes from a database schema
---example C# classes where enumerations, string constants and properties are created dynamically
-Build powerful HTML controls easily
---example create JavaScript and HTML code dynamically from CLR objects
-Build WPE/E (SilverLight) controls
Code Creator details:
- less than 1000 lines of C# code
- few basic functions – Replace, Repeat, Declare, Evaluate
- Accepts data from XML or CLR objects (very much like WPF).
- Built primarily on
--> “StringBuilder” class.
--> Tiny Interpreter class.
Tiny Interpreter (C#)
- Implementation of “Gang of 4” interpreter class
- Uses 80 line method employing REGEX to tokenize string
- Less 1000 lines of code
- Evaluates mathematical and logical expressions
- Example:

Demonstrated at Ottawa Demo Camp
Also see Creating as SilverLight Control with CodeCreator for an example on using CodeCreator.
Code Creator supports the creation of almost any type of code including XML.
Code Creator competes very well with PHP in the .net environment simply because it leverages .net's power. Above all Code Creator is small and simple.
An example of CodeCreator creating C# code segment.
// In a real system "nameUC" and "name" would get dataWill result in code be generated below:
// from a database or XML data file.
// "nameUC" and "name" are
// passed as parameters to Code Generator.
//
string[] nameUC = new string[]{"NAME", "SYSTEM", "ADMINISTRATOR"};
string[] name = new string[] {"Name", "System", "Administrator"};
...
// The next few lines of code are passed to Code Generator
// as a string.
//
//<Repeat> -- note that Code Creator commands are
// hidden in comments.
public const string NAME = "name";
//<Replace>public const string $nameUC$ = "$name$";</Replace>
//</Repeat>
public const string NAME = "Name";CodeCreator Uses
public const string SYSTEM = "System";
public const string ADMINISTRATOR = "Administrator";
-Create typed classes from a database schema
---example C# classes where enumerations, string constants and properties are created dynamically
-Build powerful HTML controls easily
---example create JavaScript and HTML code dynamically from CLR objects
-Build WPE/E (SilverLight) controls
Code Creator details:
- less than 1000 lines of C# code
- few basic functions – Replace, Repeat, Declare, Evaluate
- Accepts data from XML or CLR objects (very much like WPF).
- Built primarily on
--> “StringBuilder” class.
--> Tiny Interpreter class.
Tiny Interpreter (C#)
- Implementation of “Gang of 4” interpreter class
- Uses 80 line method employing REGEX to tokenize string
- Less 1000 lines of code
- Evaluates mathematical and logical expressions
- Example:
<Evaluate>XCoord = XCoord + (30 * Height);</Evaluate>
Demonstrated at Ottawa Demo Camp
Labels: C Sharp
Wednesday, April 11, 2007
Top 10 Coding Guidelines for C#
Here is a set of coding guidelines that we use to generate code. Using these guidelines we strive to build software that improves the 'able' criteria such as maintainable, readable, reliable, expandable, reusable, etc. The measurement of these criteria is somewhat subjective.
It should be noted that the biggest factors that impact the 'able' criteria are made by selecting C#, .net and Visual Studio development environment. The guidelines merely refine the results.
Here is out top 10 .net C# coding guidelines.
It should be noted that the biggest factors that impact the 'able' criteria are made by selecting C#, .net and Visual Studio development environment. The guidelines merely refine the results.
Here is out top 10 .net C# coding guidelines.
- Use Object Oriented (00) principles.
- Use unit tests.
- Avoid global (or static) properties or variables.
- Make extensive use of core .net classes such as Collection and Regular Expressions
- Use strong typing and avoid the use of the "object" type.
- Use Auto-Generated Code
- Use a naming convention.
- Use Static Methods and Constants
- Use the "this" keyword.
- Use Regions to place code in Chapters and Sections.
Labels: C Sharp, CodingGuideline C#
Tuesday, April 10, 2007
Make Use of #regions When Developing C# .net Code - Coding Guidlines Series
The is part of a .net Coding Guidelines series of blog postings.
Making code readable is an important consideration when developing and maintaining code. The single best way to make code more organized, thus more readable, is to use "region" compiler directive. Regions effectively group your code into sections and chapters.
This is a simple example of code without the region directive. The code is all there but even with just a few lines it can be difficult to read because nothing stands out. This only gets worse when the class is hundreds or thousands lines long.
Here is the same code with regions. It just looks a lot cleaner.
Now when you have found the selection that you want it is simply a matter of double clicking the region tag to see the details.
(example to be added)Even code with regions can get out hand when there are a large number of regions. Then it is time to group the regions into regions.
Making code readable is an important consideration when developing and maintaining code. The single best way to make code more organized, thus more readable, is to use "region" compiler directive. Regions effectively group your code into sections and chapters.
(example to be added)Even code with regions can get out hand when there are a large number of regions. Then it is time to group the regions into regions.
Labels: C Sharp, CodingGuideline C#, Visual Studio
Monday, April 9, 2007
Tokenize a String with C# Regular Expressions
Using C# and .net regular expressions it easy to parse even the most complex string into tokens very easily.
For example convert the following string:
Here are the steps.
1. Define a Token helper class.
" give the group a name. The "\s" matches on a white space character, with the "\s*" subexpression matching on zero or more whitespace characters in a row.
Overall the pattern directs to both match on whitespace and on one of the following groups or subexpressions such as "variable", "integer". Notice the "" character (shift backslash on keyboard) after each group which is the logical 'or' in C#; however in regular expressions it is known as the 'alternation' character with each subexpression known as the alternative. When the first alternative match is found then matching stops.
The "invalid" group will match on character string not matched by any of the other alternative groups. This supports simple syntax check.
3. Generate the regular expression code.
Note: When using regular expressions it is very easy for a single character to change the meaning of a regular expression. Techniques such as code inspection rarely will reveal a problem. It is important with all code, but especially code using regular expressions, to build a good set of unit tests that exercise most, if not all, of the combinations.
For example convert the following string:
"365 + 6 *(6.3 + Count)"into something like this:
Token[0], Integer, "365"With out using regular expressions it becomes quite a programming exercise. With regular expressions it becomes quite simple. The regular expression technique even supports simple syntax checking for invalid characters or character sequences.
Token[1], Plus, "+"
Token[2], Integer, "6"
Token[3], Mulitply, "*"
Token[4], OpenBracket, "("
Token[5], Double, "6.3"
Token[6], Plus, "+"
Token[7], Variable, "Count"
Token[8], CloseBracket, ")"
Here are the steps.
1. Define a Token helper class.
public class Token2. Define the regular expression pattern using named groups looking something like this.
{
public readonly string Name;
public readonly string Value;
public Token(string name, string value)
{
Name = name;
Value = value;
} >
}
private static string pattern =The regular expression explained: The round brackets"(...)" define a group that support matching a subexpression. The "?
@"(?<whitespace>\s*)|" +
@"(?<variable>[a-zA-Z_$][a-zA-Z0-9_$]*)|" +
@"(?<integer>[0-9]+)|" +
@"(?<plus>\+)|" +
@"(?<minus>-)|" +
@"(?<multiply>\*)|" +
@"(?<invalid>[^\s]+)";
Overall the pattern directs to both match on whitespace and on one of the following groups or subexpressions such as "variable", "integer". Notice the "" character (shift backslash on keyboard) after each group which is the logical 'or' in C#; however in regular expressions it is known as the 'alternation' character with each subexpression known as the alternative. When the first alternative match is found then matching stops.
The "invalid" group will match on character string not matched by any of the other alternative groups. This supports simple syntax check.
3. Generate the regular expression code.
Regex regexPattern = new Regex(pattern);4. Perform a "foreach" on matches to generate the tokens.
MatchCollection matches = regexPattern.Matches("365 + 6 * Count");
ListThat is all that is to it. A very small amount of code to quickly parse a string.tokenList = new List ();
foreach (Match match in matches)
{
int i = 0;
foreach (Group group in match.Groups)
{
string matchValue = group.Value;
bool success = group.Success;
// ignore capture index 0 and 1 (general and WhiteSpace)
if ( success && i > 1)
{
string groupName = regexPattern.GroupNameFromNumber(i);
tokenList.Add(new Token(groupName , matchValue));
}
i++;
}
}
Note: When using regular expressions it is very easy for a single character to change the meaning of a regular expression. Techniques such as code inspection rarely will reveal a problem. It is important with all code, but especially code using regular expressions, to build a good set of unit tests that exercise most, if not all, of the combinations.
Labels: C Sharp, Regular Expressions
Thursday, March 29, 2007
Selecting Editable Items in an .net WPF ListBox (or ComboBox)
What could be simpler than selecting items from a WPF ListBox. You can even make the items have fancy formating using a TextBlock. If you want to allow the user to edit the ListBox items then there is a problem. This occurs when your ListBox items are editable objects, such as a TextBox. Selecting around the TextBox selects the appropriate ListBox item, but selecting the TextBox only selects the TextBox.
The solution to this problem is when the TextBox is selected, to also select the ListBox item. To this you need to setup an event handler and write a few lines of C# code. You want use the "GotFocus" event since the TextBox may be selected by multiple methods such as tabbing with the keyboard or clicking with the mouse.
To make things simpler you can use Expression Blend to setup the event handler and create C# shell code. To do this select the TextBox properities using Expression Blend and then select the event handlers.
Here is a C# event handler for the TextBox, which is any item in ListBox for this example.
Where the ListBox is defined as
with a template like this
The solution to this problem is when the TextBox is selected, to also select the ListBox item. To this you need to setup an event handler and write a few lines of C# code. You want use the "GotFocus" event since the TextBox may be selected by multiple methods such as tabbing with the keyboard or clicking with the mouse.
To make things simpler you can use Expression Blend to setup the event handler and create C# shell code. To do this select the TextBox properities using Expression Blend and then select the event handlers.
Here is a C# event handler for the TextBox, which is any item in ListBox for this example.
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb == null) return;
if (tb.DataContext == null) return;
this.listBoxDataItems.SelectedItem = tb.DataContext;
}
Where the ListBox is defined as
ItemsSource="{Binding Path=CodeCreatorData}"
x:Name="listBoxDataItems" />
with a template like this
GotFocus="TextBox_GotFocus"/>
Subscribe to Posts [Atom]