Category: C#

Do you know what your C# variables contain?

It might sound like a trivial question but there are several ways to analyse a variable and they might give different answers. It’s therefore good to know what the difference in-between them are. The different tools we’ll go through here are: is, typeof and GetType. To include the complexity of inherited objects we create two

Missing the timer in WPF?

It’s not gone, it has just moved away from being a UI component to become a code class. In WPF it’s called Dispatcher Timer and can be used in the following way: using System.Windows.Threading; public MainWindow() { InitializeComponent(); DispatcherTimer dispatcherTimer = new DispatcherTimer(); // Register method target to execute every tick dispatcherTimer.Tick += DispatcherTimerTick; //

Have you tried the null coalescing operator in C#??

It’s quite handy and can make the code a bit more lean if you need to do a lot of null value testing. Here is a simple example: processCar = inputCar ?? new Car(); What it basically mean is “if inputCar is null then create a new car instead”. The code can be unfolded in

Add a notification icon to your WPF program

I needed to create a WPF program that will run at certain intervals and, when running, always will do it in the background. Not that difficult. But I also wanted it to be accessible through a notification icon down in the right corner on the Windows screen. A little more work, but not that bad.

Complex syntactical sugar using yield in C#

Yield is one of the available contextual keywords in C#, meaning it has a purpose in the code but it’s not a reserved word. Microsoft defines yield as being: Used in an iterator block to return a value to the enumerator object or to signal the end of iteration The keyword was added in .NET

Implement a complex type in MVC4 and EF

When you create models in Entity Framework you have the possibility to generalize parts of it into reusable types by adding the ComplexType attribute. To illustrate I have a made up example of an access definition type that can be used on many different user-to-something connections to define what kind of access the user has.

How to bind a WPF ListView to a list

As with many controls in .Net, binding is a powerful way to display data from a data source. In this simple example we’ll show you how to bind a ListView to a list of objects. We’ll start by defining the data we want in the list by creating a class with properties. In this simple

Security in MVC4

Traditional security in web applications has focused on securing a file or folder through web.config. The problem with this security model is that a path is unreliable when using routing. Two different routes can lead to the same function and thus both routes need to be secured. This opens up for security holes. When looking

WPF data change notification from custom classes

When binding a WPF control to a custom-made class, changes to property values in the object won’t automatically appear in the control because the control can’t by default recognize changes. The solution is to let the class implement the System.ComponentModel.INotifyPropertyChanged interface. The Interface exposes one event that you execute every time a property changes value.

Close program from main constructor

Sometimes you realize in the constructor that some criteriums are not fulfilled. Maybe there is no network access or something similar. The problem is that you can’t run the normal Close() for the window. One simple way of getting around this is to create an EventHandler for the Loaded event because when that event is

%d bloggers like this: