Chapter 5: Expressing Ideas

Just for something to do, I decided to clean up some code. Here s what happened ”not all of it good!

Something to Do

I think I ll refactor the code a bit, just for something to do. Let s take a look. Here s the method that handles Enter:

 public void Enter() { 
int cursorLine = CursorLine();
String[] newlines = new String[lines.Length+2];
for(int i = 0; i <= cursorLine; i++) {
newlines[i] = lines[i];
newlines[cursorLine+1] = "";
newlines[cursorLine+2] = "<P></P>";
for (int i = cursorLine+1; i < lines.Length; i++) {
newlines[i+2] = lines[i];
lines = newlines;
selectionStart = NewSelectionStart(cursorLine + 2);

Here are some of the ideas that went into this code:

  • Handle the Enter key.

  • Change the lines table to include a blank line and a P tag line after the line that contains the cursor.

  • Figure out which line contains the cursor.

  • Copy the lines from the beginning through the cursor line to the output.

  • Put a blank line into the output.

  • Put a P tag line into the output.

  • Copy the lines from after the cursor through the end to the output.

  • Place the cursor in the middle of the P tag.

  • Remember the output lines.

You might be wondering why I m mentioning the inclusion of the blank line and the P tag twice. The first mention is the purpose of the whole method. The second is a description of what some lines of code do.

Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291 © 2008-2017.
If you may any questions please contact us: