Tag Archives: QuickNote 1.0

C#: Day 4

Yesterday, I finished the 24 episode, 7+ hour, Channel 9, C# Fundamentals: Development for Absolute Beginners. While I could have been done by Day 3, or having done an extra episode or two each day, I chose to do it in a total of four. That allowed me to blog about it since Day 1, write the actual code myself, do the adequate note taking and commenting, go back and review what I had written, and do a little programming of my own. I think that was a good pace. Next is the 35 episode, 11+ hour, Channel 9, Windows Phone 8 Development for Absolute Beginners. Having completed this series, I should be prepared enough (I certainly am excited enough).

Finally, lesson 18 through 24, were especially rewarding and captivating (not that the previous seventeen weren’t). My admiration of the IDE has certainly increased as it is very helpful having automatic indentation when writing code around previous code and then with Enumerations and what Bob Tabor refers to as IDE magic: It truly is magical having an entire code block inserted for you that you otherwise would have typed yourself. Microsoft Visual Studio Express 2013 is an exceptional Integrated Development Environment.

Although I don’t go into detail of what is covered in the series from lesson to lesson, Exceptions was a heartfelt topic as I have been thorough with it ever since programming the ARexx language and through the Assembly language as well as ActionScript. Really, I think it should be Etiquette (deviquette?). Events were very familiar too of course, being what Bob refers to as the drive of a Graphical User Interface (GUI) application, which is close to what a few Doors were, with their Text-based User Interface (TUI), and also what ActionScript often is for, interactivity.

Concluding, below is the source code I wrote off the top of my head, summarizing C# Fundamentals: Development for Absolute Beginners.

I initially thought of Collections, Objects, ClassesMethods and LINQ, although, to obviate the beginner syndrome, chose the straightforward approach of the Framework Class Library (FCL), specifically the Base Class Library (BCL) and fundamental Statements, Expressions, Operators, Operands, StringsArrays, EventsStatic Classes and Static Class Members, Methods and Properties. Undoubtedly, there is a preferable approach to the logic I did and possibility to refactor the code I wrote, nonetheless, it does what it is intended to do.

I had an encounter with the “Index was outside the bounds of the array” error of which I set a breakpoint for debugging. It was satisfying to step over each statement, through each iteration and the branching, observing the Locals and Error List window and to pin variables and their value to the source (Debugging was something I used to like while programming the Assembly language too). A subsequent Bing of the error message and I knew what to look for.

This is a Windows Presentation Foundation (WPF) application (Native Windows Application), with a window of which you have a textbox to type in, a button to click and a textbox of which will output how long it took to type and the character total. The intent is to type a text without an ensuing space after a punctuation and a lowercase subsequent initial letter. When you click the button, it will insert a space after each punctuation and uppercase the subsequent initial letter into a capital letter. I contemplated whether to decipher acronyms and abbreviations too.

Screenshots of the WPF Application.

The C# and XAML WPF Application before and after a click of the Fix button.

The auto generated XAML in the IDE from dragging Controls from the Toolbox as well as from the use of the Properties Window was remarkable.

XAML

<Window x:Name="QuickNote" x:Class="QuickNote.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="QuickNote © Spiritus et Technologiae" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="Notes" Height="255" Margin="10,10,10,0" TextWrapping="Wrap" Text="start typing here.click fix afterwards." VerticalAlignment="Top" ToolTip="Don't think of a space after punctuation or a subsequent capital letter. You do have to separate each word with a space and a sentance with punctuation though."/>
        <Button x:Name="Fix" Content="Fix" HorizontalAlignment="Left" Margin="220,280,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" ToolTip="Add a space at the end of each sentance and capitalize each initial letter."/>
        <TextBox x:Name="ElapsedTime" HorizontalAlignment="Left" Height="23" Margin="348,279,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="151"/>
    </Grid>
</Window>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace QuickNote     // © Spiritus et Technologiae
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DateTime startTime = DateTime.Now;                          // Start the timer when the application launches

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //string textInput = Notes.Text;                        // Get the note into variable "textInput" of data type "string"

            char[] charArray = Notes.Text.ToCharArray();            // An array with the characters of the input
            string arrayChar = "";                                  // Declaration statement
            string nextChar = "";
            Notes.Text = "";
            int i = 0;                                              // A pointer for the next character in the array
            bool y = true;                                          // A flag, if true then uppercase character

            foreach (char Char in charArray)                        // Iteration statement
            {
                if (i == charArray.Length - 1)                      // If there aren't any characters after the current character
                {                                                   // then break, preventing "Index was outside the bounds of the array"
                    break;
                }
                else
                {
                    i++;                                            // Increment the pointer each loop through
                    nextChar = charArray[i].ToString();             // Get next character into variable "nextChar", explicitly convert to
                                                                    // string, increment pointer to point to next character consecutively
                    arrayChar = Char.ToString();                    // Get the current character into the variable "arrayChar"

                    if (arrayChar == ".")                           // Following code is self-explanatory:
                    {
                        if (nextChar == " " || nextChar == ".")     // Decision statement
                        {
                            Notes.Text = Notes.Text + ".";
                            y = false;
                        }
                        else
                        {
                            Notes.Text = Notes.Text + ". ";         // Concatination
                            y = true;
                        }
                    }
                    else
                    {
                        if (y == true)
                        {
                            Notes.Text = Notes.Text + arrayChar.ToUpper();
                            y = false;
                        }
                        else
                        {
                            Notes.Text = Notes.Text + arrayChar;    // Expression statement
                        }
                    }
                }
            }

            Notes.Text = Notes.Text + nextChar;                     // The last character

            //string addSpaces = textInput.Replace(".", ". ");      // Replace punctuation with a punctuation and a space in the string
                                                                    // "textInput" to a new string "addSpaces"
            //Notes.Text = addSpaces;                               // Output the note with added spaces to the "Notes" textbox

            TimeSpan totalTime = DateTime.Now.Subtract(startTime);  // Get total time since "Fix" was last clicked or application launched

            ElapsedTime.Text = String.Format(
                "{0} s. and {1} char(s).",                          // Format and explicitly convert timer and counter and output it
                                                                    // to the textbox "ElapsedTime"
                Math.Round(totalTime.TotalSeconds).ToString(),      // Chaining
                Notes.Text.Length.ToString());

            startTime = DateTime.Now;                               // Reset the timer when "Fix" is clicked
        }
    }
}

Get the executable program in a zip archive here. In addition, get the Channel 9 Windows 8 app here, like the Microsoft Visual Studio Facebook page here and get Visual Studio Update 1 here.

Advertisements