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.
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.
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.
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!
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:
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:
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.
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!