Category Archives: C#

C# Open File Dialog

      1 Comment on C# Open File Dialog

// Configure open file dialog box Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Title = “Select File”; // dialog title dlg.FileName = “”; // Default file name dlg.DefaultExt = “.csv”; // Default file extension dlg.Filter = “CSV File (.csv)|*.csv|All Files (*.*)|*.*”; // Filter files by extension // Show the dialog and process… Read more »

Almost Equal C#

      No Comments on Almost Equal C#

Test if two values are almost equal. /// /// Test if two doubles are approximately equal /// /// Test variable one /// Test variable two /// epsilon a measure of equality /// boolean true = values are approximately equal, false = values are not equal public static Boolean almostEqual(double a,… Read more »

Using OpenCV inside a C# WPF application

      1 Comment on Using OpenCV inside a C# WPF application

In this example I will create a C++ dll that with contain the OpenCV image processing code. I then will create a C# WPF application, within this application i will then include the dll and using the image processing function. Install OpenCV Create a C++ Win32 console Application, in… Read more »

Simple Write File using C#

      No Comments on Simple Write File using C#

A very simple method for writing to a file, not forget to include using System.IO;. string filename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @”\test.txt”; string content = “This is the new file content”; File.WriteAllText(filename, content);

Data Validation and Exceptions in C#

      No Comments on Data Validation and Exceptions in C#

The setName funcion demostrates simple data validation on the argument value. void setName(string value) { // validate empty if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentNullException(“Please enter a value”); } // validate length if (value.Length > 10) { throw new ArgumentException(“The value is too long”); } // value is valid MessageBox.Show(“The value… Read more »

C# class properties

      No Comments on C# class properties

The code below shows the short hand and long hand syntax for defining class properties. public class Example { // class property short hand public string Firstname {get; set;} // class property long hand private string _Lastname; public string Lastame { get {return _Lastname} set {_Lastname = value} } }

Simple C# LINQ example

      No Comments on Simple C# LINQ example

The LINQ example below creates a generic collection of the class Car. Then using a LINQ statement the collection is filtered to find cars that are newer than 2009. List myCars = new List() { new Car() { Make=”BMW”, Model=”550i”, Year=2009 }, new Car() { Make=”Toyota”, Model=”4Runner”, Year=2010 }, new… Read more »

Adding days to a DateTime in C#

      No Comments on Adding days to a DateTime in C#

Add seven days to the current time. DateTime start = DateTime.Now; Console.WriteLine(“Start date = ” + start.ToString(“dd/MM/yyyy hh:mm:ss”)); DateTime end = start.AddDays(7); Console.WriteLine(“End date = ” + end.ToString(“dd/MM/yyyy hh:mm:ss”)); Other methods for defining a DateTime object DateTime start = DateTime.Parse(“23/01/1984 01:02:03”); DateTime start = new DateTime(1984, 01, 23, 01, 02,… Read more »