Tuesday, 11 September 2012

Respect to System.Windows.Input

For it brings us the RoutedCommand.  RoutedCommand is a central pillar to Commanding in WPF and implements the ICommand interface (also part of System.Windows.Input) with which every .NET programmer should be really familiar with. ICommand? Oh yeah, that old thing. That means the RoutedCommand has to implement Execute and CanExecute, is all. And not just RoutedCommands, either. Also RoutedUICommands, which is, like, a special case of a RoutedCommand. I won't explain the differences between the various RoutedCommands. Let experience be your teacher.

Recap: Name three classes in System.Windows.Input related to Commanding. Hint: See Above.

But one thing we must mention.

System.Windows.Input ain't just about Commanding. 

Oh no, there's a whole PLETHORA of crazy stuff in there. Everything to do with the WPF input system, including keyboard, mouse and stylus input processing types. One such crazy class would be e.g. StylusButton, that represents a button on a stylus. Much cooler than going pound-include iostream; and all that malarkey in C++. Forget C++. This is the Age of C#.

Monday, 13 August 2012

Art of C# Sockets II: "Lineage" of NetworkStream

What is a NetworkStream? Specifically what makes it unique from its abstract base class Stream.

A network stream is like a stream of water, only instead of H20 going from the top of the mountain to the valley below, we instead have "molecules" of data (which we call bytes)  travelling continuously from a source to a destination. If the source and desination are at different parts of a computer network, the connecting byte stream can be loosely termed a "NetworkStream".

Ok, so we know vaguely know what a Network Stream, as they call it, is all about. Where does the NetworkStream class come from? What's its lineage, so to speak, in terms of the object model/class hierarchy.

Stream is the abstract base class of all Streams (the top of the Himalayas, if you will). Stream is the only class that NetworkStream directly subclasses from. Indirectly, NetworkStream also inherits from MarshalByRefObject and System.Object but these are rather peripheral facts (we'll look at the esoteric implications much later).

NetworkStream is a specialization of a Stream that supports reading, writing and seeking for Streams over the Network and lives in the System.Net.Sockets namespace. The class is very useful when writing TCP clients and servers.

NetworkStream extends System.IO.Stream. Has methods to read and write byte arrays. Streams also provide seeking capabilities - ability to query and modify the current position within a stream.

What is the practical usage of NetworkStream?

To send messages from TCP client to server.

C# Sockets Programming (Intro to System.Net.Sockets)

Desperate Programmer: I want to write network software and not be bound by weirdo libraries that limit me in how many connections I can make and such like.
Wise Programmer: Then what you need is Sockets. Master sockets and cross the impasse of data transfer across networks.
Adage: Don't trust a Dot Net Programmer to architect an application who has never programmed network software in C# and moreover has never programmed Sockets.
Desperate Programmer: So, then how do I unlock the Sockets functionality of .Net?
Wise Programmer: Start with the System.Net.Sockets namespace and progress from there

Concept Check: What is the Relation between TCP and Sockets?

Socket is a SOFTWARE OBJECT -> that REPRESENTS an endpoint for inter-process communication. Sockets can use various PROTOCOLS (rulesets), such as TCP and UDP.

How do you create a TCP Client in C#? Tell me PRECISELY.

The answer is SIMPLE. You can guess it without knowing C#. Use the TcpClient class. Where does that great TCP client class live? Ans: System.Net.Sockets (part of System.dll).

Suppose your TCP client is aptly named client. To connect the client to a socket, you need to do client.Connect(IPEndPoint). The endpoint consists of an IP address and a port. Now we have a connection. The server has to be up though for this connection to work.

How do you send a message from the Client to the Server?

You need to grab the NetworkStream from the TcpClient.  You can do stuff with the Stream, like write byte arrays to it, and Flush it. To convert a string to a byte array, you can use the class ASCIIEncoding (System.Text, from mscorlib.dll) which represents an encoder. Calling GetBytes on the encoder creates a byte buffer.

How do you create a TCP Server in C#? Again, a precise explanation is requested.
This is just a TcpListener. You create a TcpListener using IPAddress.Any, and the required port.  Calling Start() on the listener starts it listening to incoming requests. The method AcceptTcpClient returns the next TCP client accepted by the listener. Once you have the client object, you can spawn a new thread to handle the client communication.

Writing a TCPClient is child's play. Writing a TCP server is a little more involved.

Friday, 27 July 2012

A Guide to C# Interfaces for the Dotnet Architect

When drawing up an interface, one may wonder: "What are the valid constructs I can include in a C# interface?" An ARCHITECT of .Net software must be clear on this point, to be able to design interfaces that are a) SYNTACTICALLY CORRECT b) FIT FOR PURPOSE.

The answer is not as obvious as it may seem.

What CAN be put into a C# Interface? (MPE IND - Mumbai-Pune Expressway, India)
Methods, certainly.  Properties, yes. Events and indexers too - but not operators - why, because operators are declared static, and C# has no mechanism to require a type to implement static methods. The concept of interfaces in C# is thus much richer than the same construct in Java (which supports declaration of methods and constants).

What CANNOT be put into a C# Interface?
The reason why C# uses static operator overloading is based on cost/benefit analysis (read this piece from the C# compiler team. The question naturally arises since in  C++ operator overloading is done at instance level rather than static level (doesn't use the static keyword)). The upshot (or rather downshot) of this is that operator overloading cannot be prescribed at the interface level.

An Interface Specifies a Contract
A non-abstract implementation of an interface MUST implement ALL its members.

Accessibility Modifiers and Interfaces (Restaurant Example)
By definition, all interface members are public, since an interface is the public visage of a class or struct, it is what the class or struct wants to show to the outside world. (Like a restaurant that allows customers to orderFood, payBill, addTip and so forth). It may also have properties such as Address, StyleOfCuisine and so on. It may not expose the method CookFood unless clientele are able to cook their own food, for example using a grill built into the table. That may be part of the implementation but not part of the interface. An indexer may not be appropriate, unless be are creating an interface for RestuarantChain and want to access particular outlets of the chain. Events could be things like: CallForTakeawayOrder.

Boost Math Basics

Understanding Boost Math is about a) understanding the underlying mathematical concepts, but additionally you have to understand b) what the typenames mean.

Quick revision guide on typenames in C++: They are used to disambiguate type declarations, but have a dual role in that they can be used in place of class in template parameter declarations.

Math typenames from Boost:
1. RealType - denotes a real number, usually represented/approximated by, a double.
2. Policy - transmits behaviour, e.g. what to do when certain error conditions are met. An example in STL of a policy class would be std::allocator which communicates memory management policy to container classes.

You also need to understand error functions, mathematically denoted by erf(z) and erfc(z)=1-erf(z). erfz is e to the minus t squared integrated between 0 to z and "normalized" by two divided by the square root of pi.

The error function (also known as the "Gauss error function") is one of the "special functions" in mathematics. It has a sigmoid (or "S") shape.

You will see also that Boost header files have a .hpp extension rather than .h (which signifies pure C++). .h files are also used in C.

Wednesday, 18 July 2012

Inverting the STANDARD Cumulative Normal Distribution in Excel and C++

For various financial applications, you will need to invert the cumulative standard normal distribution. The correct Excel worksheet function for this is:

NORMSINV

You need to give it a number between 0 and 1. The function NORMSINV is confusing, because the name does not allude to the fact that we are inverting the CUMULATIVE normal distribution. (NORMINV is the cousin of NORMSINV that inverts the Normal Distribution more generally. Consequently, mean and standard deviation parameters must be specified explicitly. NORMSINV can be regarded as a special case of NORMINV that just accepts a probability as parameter).

Here is an algorithm for computing the inverse cumulative normal distribution. Note that the function is a continuous, non-linear (curve rather than a straight line) function and maps the (0,1) open interval on the x-axis into the entirety of the real line (although most visibly between -3 and 3 on the y-axis). It's very important to understand visually what this function is, to avoid confusing it with any other related function.

If you want to do this in Visual C++, then the Boost Math library (written by John Maddock and Paul Bristow) will do the needful.

Boost implements a generalized version of this, using the notion of Quantiles. A quantile can be thought of as the inverse of a Cumulative Distribution Function, returning a value x such that cdf(dist, x) == p. The quantile is also known as the percent point function, or percentile, or the lower critical value of the distribution.

Summary: Quantile = Inverse CDF

To understand the Boost API, we should also understand what is the complement of the cumulative distribution function. This is the probability that a variable take a value greater than x.

Thursday, 12 July 2012

Operator Overloading in C#

A feature from C++. Operands can be custom classes or structs. syntactically just a static method with keyword operator immediately preceding the operator to overload, then the operand params. Exs. +, -, ++, % (modulo), == etc.

Example from MSDN:
public static Complex operator+ (Complex c1, Complex c2)

If you are implementing your own collection classes, you may want to add support for subscripting into the collection. In this case, you cannot "overload" the subscript operator, but instead must create an INDEXER.

An indexer works as follows.

class MyCoolCollectionWithIndexerSupport<T>
{
   public T this[int i]
  {
    get { return _arr[i]; }
    set { _arr[i] = value; }
  }
}

(this is an interesting use of the this keyword. commonly this is used to refer to the current instance of a class, and as a qualifier for variables within a class. static member functions do not have a this pointer).