Skip to content

Instantly share code, notes, and snippets.

@wolf99
wolf99 / BoilerplateException.cs
Last active August 3, 2017 10:08
A boilerplate C# exception class
using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace FooProgram
{
// Recommended practice to derive from Exception rather than ApplicationException
// Recommended practice to make exceptions serializable
[Serializable]
public class BoilerplateException : Exception, ISerializable
@wolf99
wolf99 / ConvertWithSign.vb
Last active April 20, 2017 12:33
VB.NET extensions to allow conversion from an unsigned type to the equivalent signed type, and vice versa, while preserving the sign bit, by using a structure with explicit layout to simulate a union.
Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
Imports System.Runtime.CompilerServices
Module ConvertWithSign
' These extensions allow conversion from an unsigned type to the equivalent
' signed type, and vice versa, while preserving the sign bit.
'
@wolf99
wolf99 / GetDictionaryOfProperties.vb
Last active May 5, 2017 08:15
VB.NET function to get a Dictionary of the names and values of the properties of a given object
Option Strict On
Option Explicit On
Imports System.Reflection
Module Gists
Public Function GetDictionaryOfProperties(ByRef o As Object) As Dictionary(Of String, Object)
Dim Properties As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
#If Not USE_LOOP
@wolf99
wolf99 / GetDictionaryOfProperties.cs
Last active May 5, 2017 08:15
C# function to get a Dictionary of the names and values of the properties of a given object
using System.Reflection
public class Gists
{
public Dictionary<string, object> GetDictionaryOfProperties(o As Object)
{
Dictionary<string,string> Properties;
Properties = (from x in o.GetType().GetProperties() select x).ToDictionary
( x => x.Name, x => x.GetGetMethod().Invoke (o, null));
@wolf99
wolf99 / isthreads.c
Created May 12, 2017 09:41
Quick C program to detect if standard C threads are supported by checking the `__STDC_NO_THREADS__` macro
#include <stdio.h>
int main (int argc, char *argv[])
{
// Get the compiler name and version, where easily obtained, for a more
// informative output
char *compilerName;
char *compilerVersion;
#if defined(__clang__)
compilerName = "Clang/LLVM";
@wolf99
wolf99 / TobyForTabsFaq.md
Last active April 12, 2020 16:48
TobyForTabs - FAQ For Fun

What is Toby?

Toby is grouchy. Toby wonders why you think you need so many damn tabs open at the same time anyway.

How does Toby work?

By finishing stuff (and with coffee). Then I can close some of those tabs.

Who uses Toby?

Hello magazine says that people that like to be in control of everything all of the time (like browser tabs) might be using Toby.

What are the benfits of using Toby?

@wolf99
wolf99 / BoilerplateINotifyPropertyChanged.cs
Created May 31, 2017 10:26
A boilerplate C# implementation of INotifyPropertyChanged
using System;
using System.ComponentModel; // For INotifyPropertyChanged interface
using System.Runtime.CompilerServices; // For CallerMemberName, could use nameof() otherwise
namespace FooProgram
{
public class BoilerplateNotify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
@wolf99
wolf99 / PaddedVersionToString.vb
Created June 9, 2017 12:38
A ToString() extension to the System.Version class, that allows specification of padding of the version parts
Option Strict On
Option Explicit On
Imports System.Text
Imports System.Runtime.CompilerServices
Module PaddedVersionToString
''' <summary>
''' Converts the value of the current Version object to its equivalent
''' String representation. Specified counts indicate the number of
@wolf99
wolf99 / FilePathToFileUrl.cs
Created August 9, 2017 14:51
method to convert a file path to a URI
// The System.Uri class can convert paths to URIs using new Uri(pathString).AbsoluteUri;
// However, that does not work if the path contains some characters, e.g. the percent
// sign or spaces. This method corrects that, it is taken from, explained and MIT
// licensed, here: https://stackoverflow.com/a/35734486/1292918
public static string FilePathToFileUrl(string filePath)
{
StringBuilder uri = new StringBuilder();
foreach (char v in filePath)
{
@wolf99
wolf99 / BoilerplateSingleton.cs
Created September 1, 2017 11:43
C# singleton class pattern
namespace Foo
{
/// <summary>
/// A class showing the bolierplate code for a singleton
/// </summary>
public sealed class BoilerplateSingleton // sealed to prevent derivatives adding non-singleton copies
{
// this class is a singleton, this field holds the single instatiated
// object. It is instantiated lazily, only being created when a
// reference to the object is requested via the Instance property