My favourite git commands

Check in all changes that you made

git add -A
git commit -m “Add your commit info here”
git push

Staging

Before check in you move your changed files from ‘Working Copy Changes’ to ‘Staged Changes’.

  • git add .   This stages all changed files.
  • git add -A   This stages everything (modified or deleted or …).

Revert all changes that you made (that are not pushed / committed)

git checkout .

Revert new files that you have added (changes to the index)

git reset

Revert a committed change

git revert SHA

Guides

C# Replace method

The .Replace function is a string method used to replace characters or strings in a string with another character/string else. In its easiest form you would type something like: string.Replace(“replacethistext”,”withthistext”).

Replace(string,string)

String text = "My favourite colour is red.";
string correctedText = text.Replace("red", "green");
Console.WriteLine(text);
Console.WriteLine(correctedText);
Console.ReadKey();

Will result in:
My favourite colour is red.
My favourite colour is green.

Error handling

Beware that you can have two exceptions, both triggering on the string you want to replace. Below you can find example and the exceptions it could throw.

string longText = "Bacon ipsum dolor sit amet salami pork belly tail tongue pancetta, pork loin tri-tip drumstick bresaola shankle. Bacon ham hock pork belly, sausage tri-tip tongue strip steak fatback.";
longText = longText.Replace(null, "ribeye");
Console.WriteLine(longText);
longText = longText.Replace("", "ribeye");
Console.WriteLine(longText);
Console.ReadKey();
ArgumentNullException This exception occurs when the ‘find’ or ‘replace’ value is null. This could happen if you pass strings from somewhere else, and for some reason it did not get instantiated.
ArgumentException Occurs when the replace is an empty string.

Replace with nothing

If you want to remove all occurrences of a defined string in another string, this function is a possibility (and is how I originally discovered this method). It really is quite good at removing, just use empty or null as replace value.

string text = "This is a shortXKDJIDF line of words.";
Console.WriteLine(text.Replace("XKDJIDF", null));
Console.WriteLine(text.Replace("XKDJIDF", ""));
Console.ReadKey();

Will result in:
This is a short line of words.
This is a short line of words.

Performance?

I would dare say that performance issues are negligible when using the Replace function. However I can share with you that overwriting your string with that string.Replace(something, something), is slower than assigning it to a new string.

There also is an interesting article about performance and measurement on dotnetperls.

Sources

  • http://www.dotnetperls.com/replace
  • http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

Delegates

A delegate is a special type, where you can store a reference to a function. With delegates you change the way how you work, and somehow reverse it. Give it a function, and after that you give if the requested values for that function, and process it.

Why? So you can assign an action (or maybe with if’elses or a for loop, different actions) and then after that give it the values and process it. Even you might not understand it yet completely, this makes code easier, and things possible that maybe wouldn’t be possible without this.

This sounds a bit crazy, but lucky enough it’s rather easy, you just need to understand it. Nothing better than some example to make you understand!

Continue reading “Delegates”

Example Observer Application: Traffic Lights

This is the post where you have all been waiting for (I hope J), after reading the informative post what an Observer Pattern is. If you have no idea what an observer is, you better start reading my previous theoretical post about the Observer Pattern.

First an overview of the application we are going to build in this post:

  • An application to create a traffic light who changes his colour
  • Easy expandable
  • Future equipped (easy to add changes)
  • Object Oriented
  • With the Observer Pattern

Stage 1: our Model Control View

Control Model View
Program.cs
TrafficLightApp.cs
Observable.csIObserver.cs
TrafficLights.cs
Light.cs
StartView.cs
Crossing.cs

Program.cs: The start file from the program. It creates a TrafficLightApp Object.
TrafficLightApp.cs: The main control of our program. This class covers for all the interactions. It also starts the views.

Observable.cs: This is the file where every model that needs to be observable will inherit from.
IObserver.cs: An interface where everything that listens to our Observable needs to implement.
TrafficLight.cs: Contains 4 lights (like on a crossroad).
Lights.cs: 1 light.

StartView.cs: A window forms class that has a button to create a new crossing.
Crossing.cs: Enables you to see a crossing with 4 lights.

Stage 2: Implementing the Observable and IObserver

The observable.cs should look like the following:


namespace ObserverExample
{
class Observable
{
private List allObservers = new List();
public void notifyObservers()
{
foreach (IObserver o in allObservers)
{
o.Update(this);
}
}
public void addObserver(IObserver o)
{
allObservers.Add(o);
}
public void removeObserver(IObserver o)
{
allObservers.Remove(o);
}
}
}

So basically, this class (Observable.cs) holds a list from Interfaces. When the notifyObservers() method is triggered, it accesses the method the method of all the interfaces.

So maybe, the IObserver.cs (interface) might like interesting for you to:


namespace ObserverExample
{
interface IObserver
{
void Update(Observable o);
}
}

Stage 3: Implementing the rest of the model

TrafficLights.cs and Light.cs, I will not show code extracts from them here, instead you are able (see bottom of the page) to download a zip file with the project included. You can check them there. Simple explanation what they are about? Light is just 1 light, and has a status which reflects the colour the lamp has (Red, orange or green). Each Light also has a changeLight method what changes the current colour to the next.

TrafficLights has a collection or list<> from 4 Lights. It has a method to change all the 4 their (colour) states. This class is also Observable, so it extends Observable.

Stage 4: Completing the control

I already (from the beginning) did some changes. I edited the Program.cs, to this:


static class Program
{
[STAThread]
static void Main()
{
TrafficLightApp Tla = new TrafficLightApp();
Tla.ExecuteProgram();
}
}

The TrafficLightApp() starts our StartView and also starts new windows, does the ‘control’, the hard work, makes it all works. If you wan’t to see this code, again, download the zip (at the bottom of this page).

Stage 5: Implementing the view

StartView.cs

I just created a really simple interface, with a button that enables to add a new crossing (That has 4 lights, and the ability to change the state). It looks like what you see at the right of this page.


namespace ObserverExample
{
public partial class StartView : Form
{
private TrafficLightApp tla;
public StartView(TrafficLightApp Tla)
{
InitializeComponent();
tla = Tla;
}
private void btnCreateNew_Click(object sender, EventArgs e)
{
tla.addCrossing();
}
}
}

Crossing.cs

Each Crossing contains 4 lights, and looks really nice. Pressing the next will change the status in the class light.cs. Then an notifyObserver() get’s done, and this view, wich is an Observer, will update.

Below you will find the most important parts of the code:

Update:

void IObserver.Update(Observable o)
{
Synchronize();
}

Synchronize:

public void Synchronize()
{
pnlPlaceholder.Controls.Clear();
foreach (Light light in lights)
{
Panel pnl = new Panel();
pnl.Width = 50;
pnl.Height = 50;
switch (light.Status)
{
case 0: pnl.BackColor = Color.Green;
break;
case 1: pnl.BackColor = Color.Orange;
break;
case 2: pnl.BackColor = Color.Red;
break;
case 3: pnl.BackColor = Color.Red;
break;
}
pnlPlaceholder.Controls.Add(pnl);
}
}

And the construct:

public Crossing(TrafficLights Tl, TrafficLightApp TLA)
{
this.tl = Tl;
this.tla = TLA;
lights = new List<Light>();
lights = Tl.Lights;
tl.addObserver(this);
InitializeComponent();
Synchronize();
}

The magic

The files

You can download the Source Files!

I’ve also created another zip file with the exe, and you can find it on the same link.

Sources

I’ve learned a lot from this in the book Design Patterns from O’Reilly. Also, the idea for this example, I got from my java lessons in Howest Simon Stevin, a high school in Bruges.

Add items to the Bulleted List Control (in the code file)

My next tutorial will be a really short one. But still I have spent a really long time to find what I was looking. Maybe because it was way to easy?

I wanted to generate a BulletedList Web Control, in my code file. Inside a for-loop I wanted to add texts to my list.

[ad#Adsense]

To start, I dropped a PlaceHolder in my file.asp (The html file). Because I like drag and drop, I did it that way! You can find in your toolbox. If you don’t like that, just copy-paste the following text where you would like your BulletedList to appear:
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
After that it is time to open the code behind file. In the solution explorer, click at the plus, and then open the default.aspx.cs!

The first thing is to make our Place Holder accessible. Just behind:
public partial class Default: System.Web.UI.Page
But above:
protected void Page_Load(object sender, EventArgs e)
We type this code:
private PlaceHolder PH
{
get
{
return ph1;
}
set
{
ph1 = value;
}
}

We have to create the BulletedList outside the iterator. And inside we can add the items we want to the BulletedList. First my example:
BulletedList BltTest = new BulletedList();
foreach (XmlNode QuestionAnswers in Question)
{
BltTest.Items.Add(new ListItem(QuestionAnswers.InnerText));
}
PH.Controls.Add(BltQTest);

This is the code I did use. But maybe a more generic example helps you with your BulletedList problem!
BulletedList BltTest = new BulletedList();
for (int i = 0; i < 10; i++)
{
BltTest.Items.Add(new ListItem("This is the " + i + "th item"));
}
PH.Controls.Add(BltQTest);

Good luck with your BulletedList!