Archive for the ‘c#’ Category

How to return a value form Event in C#

public delegate bool MyEventHandler(object sender /* additional params */);
public event MyEventHandler MyEvent;
 
public void MyMethod()
{
    if (MyEvent != null && MyEvent(this))
    {
        /* TODO: do something */
    }
}
 
public void MyBind()
{
    MyEvent += new MyHandler(this);
}
 
private bool MyHandler(object sender)
{
    return true;
}

Observer pattern example in C#

This article has a simple sample of using Observer pattern. The description of this pattern you can read in other resources or you can understand the basis of the pattern exploring this example. Also, you can download an example in C#. This pattern is also known as Dependents pattern or Publish-Subscribe pattern

class Subject
{
    private List<Observer> observers = new List<Observer>();
 
    public void Attach(Observer observer)
    {
        observers.Add(observer);
    }
 
    public void Detach(Observer observer)
    {
        observers.Remove(observer);
    }
 
    public void Notify()
    {
        foreach (var observer in observers)
        {
            observer.Update();
        }
    }
}
abstract class Observer
{
    abstract public void Update();
}
class ConcreteSubject : Subject
{
    private int value;
 
    public int Value
    {
        get
        {
            return value;
        }
        set
        {
            this.value = value;
            Notify();
        }
    }
}
class ConcreteObserver : Observer
{
    private ConcreteSubject subject;
 
    public override void Update()
    {
        Console.WriteLine("Subject value is {0}.", subject.Value);
    }
 
    public ConcreteObserver(ConcreteSubject subject)
    {
        subject.Attach(this);
        this.subject = subject;
    }
 
    ~ConcreteObserver()
    {
        subject.Detach(this);
    }
}
class Program
{
    static void Main(string[] args)
    {
        var concreteSubject = new ConcreteSubject();
        var concreteObserver = new ConcreteObserver(concreteSubject);
 
        concreteSubject.Value = 10;
    }
}

As an output you will have: Subject value is 10.

Links

Observer pattern description in Wikipedia

Split string but keep separator in C#

Today I tried to split the string but preserve split characters. I didn’t find native methods for this need. If somebody know how to do it please write the comment.

Here is my code:

var separator = '@';
var result = new Dictionary<int, string>();
var key = 0;
for (var i = 0; i < testString.Length; i++)
{
    var c = testString[i];
    if (c == separator) key++;
 
    if (result.ContainsKey(key))
    {
        result[key] += c;
    }
    else
    {
        result.Add(key, c.ToString());
    }
}

Converting String to Single in C#

Some time ago, I needed to get Flash Movie sizes inside of my C# code. I googled and I found some great examples of how I can do it. It was Mike Swanson’s example project called FlashTools. I downloaded it and stated to use. While I was using this code I found a little bug that was occured depending of regional settings in the system. An error occured when FlashTools library tryed to convert String to Single. When I used English locale everything was going OK but when I changed the locale I got “Input string was not in a correct format”. A fix was very simple, I just passed the InvariantCulture as the second parameter of Convert.ToSingle method.
Read the rest of this entry »

Setting HTML for WebBrowser in C#

Today I tryed to insert HTML for WebBorwser in C#. I thought it’s a simple task. But when I tryed to use DocumentText property I got “”. Then I tryed Document.Write("...") and I got that Document is null. After these tries I started to find out the easiest way to fill the WebBrowser with HTML without writing to disk and navigating the written file. Here is my code:

WebBrowser browser = new WebBrowser();
 
browser.Navigate("about:blank");
browser.Document.Write("...");

When the WebBrowser has been made, the Document object is null. So, we should call Navigate() method to get Document available.

If You have Your way to fill the WebBrowser with HTML I’m expecting for Your comments :)