C# tutorials
11. Frequency count of array values iii
Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.
To complete this tutorial, follow these instructions:
1. Open Visual C# 2010 Express.
2. Click on New Project in the File menu.
3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.
4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).
5. Drag a Button to Form 1.
6. Drag a Rich Textbox to Form 1. Resize the textbox if you like.
7. Double-click on the button. The file Form1.cs will open with the caret ready to write code that will run when button 1 is clicked.
8. Write or paste this code where the caret is:
try
{
string str0 = richTextBox1.Text;
if (str0.Length != 0)
{
string str1 = c1.mkWords(str0);
string[] arr = str1.Split(' ');
Dictionary<string, int> dic = new Dictionary<string, int>();
dic = c1.mkCount(arr, dic);
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, int> pair in dic)
{
sb.AppendLine((string.Format("{0} {1}", pair.Key, pair.Value)));
}
richTextBox1.Text = sb.ToString();
return;
}
else
{
MessageBox.Show("Write something in the textbox and try again.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
catch (Exception excMsg)
{
MessageBox.Show(excMsg.Message.ToString(), "Error");
}
9. Write or paste this code right above public Form1()
:
Class1 c1 = new Class1();
10. Click on Add Class
in the Project menu.
11. Choose Class if that option isn't already chosen, keep the name Class1.cs and click on Add.
12. Write or paste this code right below using System.Windows.Forms;
:
using System.Text.RegularExpressions;
13. Place the caret right after the first curly brace right below class Class1
, press return and write or paste this code where the caret is:
public string mkWords(string str)
{
string prep0 = str.Trim();
string prep1 = Regex.Replace(prep0, @"\s+", " ");
return prep1;
}
public Dictionary<string, int> mkCount(string[] arr, Dictionary<string, int> dic)
{
foreach (var item in arr)
{
if (!dic.ContainsKey(item))
{
dic.Add(item, 1);
}
else
{
dic[item]++;
}
}
return dic;
}
14. Press F5 to start debugging the program.
15. Click on the button. A message box with the text Write something in the textbox and try again.
should appear.
16. Close the message box, write some words in the textbox and click on the button. A list similar to the one in 8. Frequency count of array values i
, step 5 should appear in the textbox.
Comments on the first code snippet
Line 1-27: A try block contains code that may cause an exception (an anomalous situation). A try block is executed until an exception occurs or until it is completed successfully. If an exception occurs, the catch block is executed (in this case, a message box is displayed). The type Exception represents, as far as I can tell, any exception.
Line 3: Get the text from richTextBox1 as a string.
Line 4: Check if the textbox is empty or not. If it is, display a message (line 18-22). If it isn't (in other words, if the length of the string str0 isn't 0), do line 5-17.
Line 6: Replace all whitespace with a single space (to prepare for line 7).
Line 7: Transform the string str1 to a string array.
Line 8-16: See comments on C# tutorials: 8. Frequency count of array values i.
Line 20-21: See comments on C# tutorials: 6. Hello world message box.
Comment on the second code snippet
See comments on C# tutorials: 10. Adding a class to a project.
Comments on the third code snippet
This line makes it possible to use the types in the namespace System.Text.RegularExpressions, for example Regex in the fourth code snippet, without having to specify the namespace. In other words, if you like you can add System.Text.RegularExpressions.
before Regex instead of adding a using directive at the top of the file. The upside of using directives is that you have to write less code.
Comments on the fourth code snippet
Line 3: Remove whitespace first and last in the string.
Line 4: Replace all remaining whitespace (spaces, tabs and linebreaks) with single spaces. Starting a string with @ makes the string verbatim. The advantage of verbatim strings is that you don't have to add another backslash before backslashes. In other words, @"\s+" is the same as "\\s+".
Line 7-21: See comments on C# tutorials: 8. Frequency count of array values i.
Would you like to comment on this page or some other page? Send an email to mats.kristiansson.skovde@gmail.com or a letter to Mats Kristiansson, Timmervägen 3A, 541 64 Skövde, Sweden with the title of the page you want to comment on, your comment and your name or a pseudonym.