My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.

A simple class that represents a matching quiz item This is a class that encapsulates matching items which can be displayed as a test with questions and answers and then shows the answer key. ![]()
using System.Collections.Generic;
using System; using System.Threading; namespace TestSort727272 { class Program { static void Main(string[] args) { MatchingItems matchingItems = new MatchingItems(); matchingItems.Add("one", "111"); matchingItems.Add("two", "222"); matchingItems.Add("three", "333"); matchingItems.Add("four", "444"); matchingItems.Setup(); matchingItems.DisplayTest(); matchingItems.DisplayAnswers(); Console.ReadLine(); } } public class MatchingItems { public List<MatchingItem> Collection { get; set; } public List<int> LeftDisplayIndexes { get; set; } public List<int> RightDisplayIndexes { get; set; } private char[] _numbers = { '1', '2', '3', '4', '5', '6', '7', '8' }; private char[] _letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; public MatchingItems() { Collection = new List<MatchingItem>(); LeftDisplayIndexes = new List<int>(); RightDisplayIndexes = new List<int>(); } public void Add(string leftText, string rightText) { MatchingItem matchingItem = new MatchingItem(leftText, rightText); Collection.Add(matchingItem); LeftDisplayIndexes.Add(Collection.Count - 1); RightDisplayIndexes.Add(Collection.Count - 1); } public void DisplayTest() { Console.WriteLine(""); Console.WriteLine("--TEST:-------------------------"); for (int i = 0; i < Collection.Count; i++) { int leftIndex = LeftDisplayIndexes[i]; int rightIndex = RightDisplayIndexes[i]; Console.WriteLine("{0}. {1,-12}{2}. {3}", _numbers[i], Collection[leftIndex].LeftText, _letters[i], Collection[rightIndex].RightText); } } public void DisplayAnswers() { Console.WriteLine(""); Console.WriteLine("--ANSWERS:-------------------------"); for (int i = 0; i < Collection.Count; i++) { string leftLabel = _numbers[i].ToString(); int leftIndex = LeftDisplayIndexes[i]; int rightIndex = RightDisplayIndexes.IndexOf(leftIndex); string answerLabel = _letters[rightIndex].ToString(); Console.WriteLine("{0}. {1}", leftLabel, answerLabel); } } public void Setup() { do { LeftDisplayIndexes.Shuffle(); Thread.Sleep(300); RightDisplayIndexes.Shuffle(); } while (SomeLinesAreMatched()); } private bool SomeLinesAreMatched() { for (int i = 0; i < LeftDisplayIndexes.Count; i++) { int leftIndex = LeftDisplayIndexes[i]; int rightIndex = RightDisplayIndexes[i]; if (leftIndex == rightIndex) return true; } return false; } public void DisplayAsAnswer(int numberedIndex) { Console.WriteLine(""); Console.WriteLine("--ANSWER TO {0}:-------------------------", _numbers[numberedIndex]); for (int i = 0; i < Collection.Count; i++) { int leftIndex = LeftDisplayIndexes[i]; int rightIndex = RightDisplayIndexes[i]; Console.WriteLine("{0}. {1,-12}{2}. {3}", _numbers[i], Collection[leftIndex].LeftText, _letters[i], Collection[rightIndex].RightText); } } } public class MatchingItem { public string LeftText { get; set; } public string RightText { get; set; } public MatchingItem(string leftText, string rightText) { LeftText = leftText; RightText = rightText; } } public static class Helpers { static Random rng = new Random(); public static void Shuffle<T>(this IList<T> list) { int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } } } } |
Most Recently Added Items:
- How to call an unknown function on an unknown class with unknown parameters - php code example - added on Sunday, November 28, 2010
- How to setup JQuery intellisense in Microsoft Visual Web Developer 2010 Express - jquery howto - added on Sunday, November 14, 2010
- Simple menu site with jQuery AJAX - jquery code example - added on Sunday, November 14, 2010
- A PHP function which returns the most frequently occuring item - php code example - added on Friday, October 29, 2010
- How to create a singleton in PHP - php code example - added on Sunday, October 03, 2010
- Extension method to sort a generic collection of objects - c# code example - added on Tuesday, September 07, 2010
- A simple class that represents a matching quiz item - c# code example - added on Tuesday, September 07, 2010
- Extension method for checking regex in one line - c# code example - added on Thursday, September 02, 2010
- How to use a Dictionary<> with struct key to save a dynamic matrix of objects - c# code example - added on Sunday, August 22, 2010
- A simple jquery search machine for a web page - jquery code example - added on Sunday, August 22, 2010
- Wrapper class to simplify the creation of Excel files in C# 4.0 - wpf code example - added on Tuesday, July 20, 2010
- How to make clickable flashcards in plain javascript for your mobile phone - javascript code example - added on Wednesday, July 07, 2010
- Simple example of javascript which loads jquery locally - jquery code example - added on Wednesday, July 07, 2010
- How to stop regular expression greediness - c# code example - added on Tuesday, July 06, 2010
- How to use a generic dictionary to total enum values - c# code example - added on Friday, July 02, 2010
- Generic method to case-insensitively convert a string to any enum - c# code example - added on Friday, July 02, 2010
- How to create a TextBlock that has various font formatting in code behind - silverlight code example - added on Wednesday, June 30, 2010
- How to encode binary files to text files and back to binary again - c# code example - added on Wednesday, June 30, 2010
- How to use a custom parameter struct to pass any number of variables to constructors of similar classes. - c# code example - added on Saturday, June 26, 2010
- How to make a class that renders an interactive FrameworkElement and interacts with the View - silverlight code example - added on Tuesday, June 15, 2010
- How to set a nullable type to null in a ternary operator - c# code example - added on Tuesday, June 15, 2010
- How to override events in inherited classes - c# code example - added on Friday, June 11, 2010
- How to strip off e.g. "note:" and "firstName: " from the left of a string using regex - c# code example - added on Tuesday, June 01, 2010
- How to create and subscribe to custom events using EventHandler - c# code example - added on Wednesday, May 12, 2010
- Code base for asynchronously loading and caching dependent data in a Silverlight app - silverlight code example - added on Wednesday, May 05, 2010
- Function to trim the preceding and trailing blank lines off an array - php code example - added on Sunday, May 02, 2010
- How to load and display the contents of a text file with AJAX/Jquery - jquery code example - added on Sunday, May 02, 2010
- How to use fopen() to create a proxy site to read any website content into AJAX - php code example - added on Sunday, May 02, 2010
- Code base for loading and caching external data into a silverlight app - silverlight code example - added on Friday, April 30, 2010
- An UpdateSourceTrigger workaround for Silverlight - wpf code example - added on Sunday, April 18, 2010
