Tag Archives: .Net

Call Twitter APIs from C# .Net

Twitter has an API implementation that is very simple to understand and use. Here is how it works, if you want to get the friends timeline of a user.

http://twitter.com/statuses/friends_timeline.rss

Here there are 3 keywords.
What are you working on? statuses
What do you need from the above object? friends_timeline
In what format do you need it? RSS

Formats supported are: xml, json, rss, atom
Methods supported: See Twitter API
There are many objects supported, but the above format applies for only a few like Statuses, User, Direct Messages. The rest are also similar.

Now, let us look at a GET to bring in Friend Status.

First off, we would need the URL above

The public method GetFriendsStatus calls a generic method InvokeAPI to (surprise!) invoke the API. This function in turn relies on GetUrl to build the URL for this request. AskTwitter is a helper method that does a GET request and returns the result.

As you can see, AskTwitter takes in 2 parameters. First one is the URL we built earlier. The second parameter is user credential, for requests that require credentials. This can be built easily as below.

Hope that helped get you started. Am working on a complete set of wrappers that will make using these APIs much more easier. Please leave a comment and let me know if you would be interested in any specific features.

First and Last day of a Month in C# and VB .NET

In date manipulation, one common task is to arrive at first and last day of a month. Here are two methods to achieve the same in VB and C#.

DateSerial
This is my favorite method for creating date out of numbers. The beauty is that the function accepts numbers beyond the usual range. i.e. Month = 14 would mean 2nd month of next year. Negative numbers too are accepted.

AddMonths and AddDays
In the C# world, it is not as straight-forward to use the above function. The below work-around helps.

As usual, no rocket-science being discussed here. Just small snippets to make your search life easier :)

Dynamic Casting in C#

Dynamic Casting? Well… almost. In fact, we are dynamically converting and not casting. The problem that remains is that the resultant object does doesn’t “know” at design time what it is capable of (a.k.a. not aware of the methods it has). However, this should not be a problem if there is an interface that is common to the superset of things we want to cast to.

To state an example, let us say there is an interface and 3 classes as below.

Now, we can try accessing these objects through converting at runtime. Note that the examples here might look trivial, but the application for this is when you know the type of the object only at runtime.

The output to this is as below.

DummyA has spit out the property from base as it was not overridden. However, as evident from the type reflected, the objects does get converted to desired types in each case. Also, note that the base property is declared as virtual so that it could be overridden.

In this particular case above, we already had the object and wanted them to be converted to a desired type. That scenario is different from (say), if we do not have an object and want to instantiate based on runtime information. Below is how we can do that.

Output is exactly the same as in the previous case. The difference here is that we “created” the instance in this case whereas “got” the instance in the previous one.

Recursive Permutations Generator

This is a Permutations class which will calculate all permutations of a given array, by recursively calling a method within. Usage is rather simple that you need to just do a Permuter = new Permutation(AlphaSet) and then take the Permuter.Resultset array for all permutations.

As per feedback, the Permutation class has been modified to take in variable length string arrays also, instead of just single-character string arrays as in the example here. The method adopted (of running a proxy permutation first and mapping to real array) may not be the most efficient one. But it works! ;)

Class

Usage

Recursive File List Builder

This class builds a file list out of a directory tree, starting from a specified folder. Search pattern (like *.asp, *.exe) and the level (or depth 0=all, 1=current, n=n levels) of recursion to number of sub-directories can be specified. Usage of the class is given towards the end of the article.

and the usage would be like this…

Generate Table Schema

This is a simple (but often useful) tool to generate the xml schema of a database table. The tool supports only SQL Server over Windows Authentication. The source code here can be easily extended for other uses.

Dependency Injection

This is one pattern many of us use without even realising we are using it. The premises are pretty simple.

Let’s say we are doing a sequential workflow, and that each step in the flow can be done by any among the various implementations of a particular interface. Now, we have two options – either write a controller class that loads the specific types and executes the flow (in which case we write one controller per flow) or have a configuration file that specifies the implementation at each step and the controller just go about invoking the interface. It is a no-brainer to see that the latter approach is more flexible.

In .Net, this is achieved by loading the assembly and type at runtime.

Here, IMashup is the interface and the assembly and type names are expected to come from a configuration file (or equivalent).