Archive for May, 2011

Creating event in Abstract class and Raising this event from derived class

May 28th, 2011 1 comment

I won’t explain here hat is Abstract class and why we would like to use it in our Object orientated programming. But I want to discuss some issues that come when we are using events in the base Abstract class.

When we want to create event in abstract class which intended to be raised from derived classes, we discover right away that such code won’t compile.
See following code example where created simple abstract class which have public event. Simple derived class that tries to fire this event from DoMyLogic() method.

public abstract class AbstractSample
        public event EventHandler AbstractEvent;

        public abstract bool IsActionComplete();

        public void Foo()
            //some code 

    public class MyClass : AbstractSample

        public override bool IsActionComplete()
            return true;

        void DoMyLogic()
            //Fire event that was declared in base class
            base.AbstractEvent(this, EventArgs.Empty);//compile error


Read more…

Categories: .Net, C# Tags:

Disassembly for .Net assemblies tool is here – dotPeek(decompile) from JetBrains

May 14th, 2011 No comments

dotPeek from JetBrains (makers of Resharper and many other good developers tools).

As you probably noticed that Reflector (which great tool and useful) not free any more. Here theirs announcement

I in my every day work need such tool that will provide high-quality decompiling capabilities for .Net assemblies.

dotPeek Logo

After some searches in the Internet I have found very promising tool that gives all needed functionality. Tool called dotPeek.


Tool is standalone exe file, but it gives look and feel like Visual studio with nice navigation and search features. Hopefuly they will integrate it with Resharper or as additional add-on for VS2010.
Read more…

Categories: .Net, C#, Tools Tags: ,

Deep copy against shallow copy in .Net C# dealing with mutable Structs

May 13th, 2011 No comments

In this post I won’t explain the difference between deep copy and shallow copy, what is reference type and Value type – there are plenty stuff about this in the web. I want to share with you some problematic behavior/bug that we found out in application as a result of luck of understanding in current materials.
This issue can be very tricky and hard to debug in runtime, so we need to be wide-awake.

For example you have very simple struct like follows (holds array of points which describes polygon):

public struct MyPolygon
	Point[] points;

Read more…

Categories: .Net, C# Tags: ,

How to marshall C++ reference parameter to C#.NET using C++/CLI

May 11th, 2011 No comments

Suppose you have some very complicate function written in either C or native C++. And suppose you want to use it in your C#.NET managed code. Obviously, marshalling is required.
There are generally 3 possible options for marshalling:
1) COM
3) C++/CLI
2) Pinvoke (Platform Invokation)

Since making a COM wrapper for native code (ATL e.t.c) is not trivial, and C++/CLI is usually used for more complicated cases, most used way is platform invokation: define the function as “static extern”, add “DllImport” attribute, define some parameters and – eureka! – the required managed function is ready for using…
In case of native C++ class’s method, the process is a bit more complicated, because compiler concatenates a class & method names in some strange form. Still, the new method name could be acheived using known for all “Dependency Walker” application.

Read more…

Categories: .Net, C#, C++/CLI Tags: ,

How to extend Windows Phone 7 emulator’s functionality (unlock)

May 10th, 2011 2 comments

While programming to Windows Phone 7 (or another mobile device), very important instrument is the device’s emulator, integrated into development environment. Visual Studio 2010 supplies an emulator; while compiling & running our code (using either Silverlight or XNA technology), the emulator is running and creating an environment for our application. By the way, full Visual Studio 2010 is not must for development on WinPhone7; one can use a free environment “Microsoft Visual Studio 2010 Express” instead.

Read more…

Categories: Windows Phone 7 Tags: , ,

Getting OutOfMemory exception in .Net process, possible solutions.

May 7th, 2011 No comments

Getting OutOfMemoryexception in .Net process, possible solutions.

I want to share with you very interesting and complicated problem that we managed to overcome. I will describe all our resolution procedure step by step. Hope it will help others as it helped us, in any case I have learned a lot.


We have encountered a serious problem such as OutOfMemoryException

In my application – WinForm application i have managed code and un-managed (C/C++) 3’rd party code that perform many un-managed memory allocations at beginning of the process load and during runtime.

As we started to suffer from memory problems, we decided to enlarge address space that our process can use from 2 GB to 3 GB. This operation was done by using /LARGEADDRESSAWARE flag for our process and OS configured with /3GB. Unfortunately enlarge address space to our application seems to be good solution, but it didn’t help in our case.

So we started debugging and deeply investigate our Outofmemory problems. As usual main tool that we have used was WinDbg. Interesting things were discovered by using WinDbg and using following command “!address –summary” which shows us that we have not reached 3GB memory limit. Although that we have free memory, the memory wasn’t continuous so the CLR or native code failed to allocate memory. The problem appear to be defragmentation problem.

Read more…

Categories: .Net, C# Tags: , , , , ,

Batch Count Number of Lines in Text File

May 6th, 2011 1 comment

Recently I came across the need to count the number of lines in a text file, so I decided to explore Command Shell options.

My first thought was to use Find command, but I was not able to find a new line character, so I decided to count lines, which are not containing the certain string:

find /c /n /v "***///" CreateLogTable.sql

This approach works not bad, but it opens a breach in cases when the string, which is used as a search token, can be actually a part of the file.

After some more research I found out that there is another batch command, called FindStr. This command supports regular expressions and allows us to count the number of lines without any exceptions:

findstr /R /N "^" CreateLogTable.sql | find /c ":"
Categories: General Tags: