How can I access a protected member from a derived class?

April 1st, 2008

First I encountered the error message:

Compiler Error CS1540: Cannot access protected member 'member' via a qualifier
of type 'type1'; the qualifier must be of type 'type2' (or derived from it).

Then I encountered Eric Lipperts explanation of Why Can’t I Access A Protected Member From A Derived Class?

Then I got angry. Read the rest of this entry »

A Generic C# 3.0 Collection Initialization Adapter

February 29th, 2008

C# 3.0 comes with this new language feature called Collection Initializaton. It’s more or less syntactical sugar that simplifies the way you initialize collections.

Where you pre 3.0 would initialize a list of integers like this:

List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);

You now can do this:

List<int> list = new List<int>() {1, 2, 3};

Now, this is somewhat beautified, but it does exactly the same as in the first example. The compiler parses the code and calls the Add method, implemented by List<int>, once for every integer in the initialization list.

Now, wouldn’t this be nice to do on your own proprietary collections? Read the rest of this entry »

Mimic the C# yield instruction in VC++

November 29th, 2007

Recently mickey0 posted a question on the CodeGuru forum, asking whether VC++ had any syntax similar to the C# yield instruction. Although I knew the answer was ‘no’, I was still intrigued to figure out if it was possible to implement something similar.

I started with a web search and found a couple of existing implementations, all of which were using Win32 Fibers, so I realized that fibers was the way to go. Even though the examples I found was good tutorials on fibers, they did not meet my requirements on a proper yield implementation. So I had to get dirty. Read the rest of this entry »

Static predicates, delegate inference and performance

November 24th, 2007

C# 2.0 introduced a feature called delegate inference. Delegate inference is an simplification of how you initialize delegates.

Where you prior to C# 2.0 would have to write:

MyDelegate d = new MyDelegate(MyFunction);

You can now simply write:

MyDelegate d = MyFunction;

The two lines of code behaves exactly the same. They construct a new instance of MyDelegate passing MyFunction as argument to the constructor, and then assign the instance to the variable d.

As the name ‘delegate inference’ suggests, the compiler fills in the ‘missing parts’ using inference, leaving the developer with less keystrokes and more readable code. Which is a good thing. Read the rest of this entry »

’Almost real’ functor objects in C#

October 27th, 2007

In these days of delegates, anonymous methods and lambda expressions, I’ve found it necessary to talk about good old functor objects. A functor is an object, that in some fashion you can treat as a function. Put another way, a functor is an object that you can call. Read the rest of this entry »

Lazy programming…

October 13th, 2007

I’ve been working alot with web services lately, querying for information and recordsets, applying business logics and then finally displaying it all on a web page. A problem that I need to deal with (it feels like) all the time is empty responses from the service layer.

Sometime the response is null, sometimes not. Sometimes inner collections of a response are empty, null… anyway, it end up with alot of theese:

if (products != null)
{
    foreach (Product product in products)
    {
        // do something with product
    }
}

Doing these if-nulls over and over again made me realize that I needed an NeverNullEnum wrapper, Read the rest of this entry »

Monitor your collection

July 9th, 2007

There are times when you want your class to expose some kind of collection. Maybe you got a Server class and want to world to access it connections, or you got a Family class and want to provide access to the collection of family members. Sometimes that is easy - just to return a reference to the collection from a function or property. Other times it’s not that easy.
Read the rest of this entry »

Powered by WordPress. Entries (RSS) and Comments (RSS).