Tag Archives: .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.

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.

1

NetworkCredential nwUser=newNetworkCredential("UserName","Password");

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.

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.

1

2

3

4

5

'This year, this month, first day

FirstDay = DateSerial(Today.Year, Today.Month, 1)

'Thisyear,next month,0thday isthismonth's last day

LastDay = DateSerial(Today.Year, Today.Month + 1, 0)

'See:http://msdn.microsoft.com/en-us/library/bbx05d0c.aspx

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

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//Interface

publicinterfaceIDummy

{

stringEcho{get;}

}

//Base Class, implemented from interface

publicclassDummyBase:IDummy

{

publicvirtualstringEcho{get{return"Dummy Base";}}

}

//Inherited class, without an override on property

publicclassDummyA:DummyBase

{

publicstringEcho{get{return"Dummy A";}}

}

//Inherited class, with an override on property

publicclassDummyB:DummyBase

{

publicoverridestringEcho{get{return"Dummy B";}}

}

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.

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.

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! ;)

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

using System;

using System.IO;

using System.Collections;

namespaceSpider

{

/// <summary>

/// Summary description for FileList.

/// </summary>

publicclassFileList

{

#region Vars

privatestring_Path;

privatestring_Pattern;

privateint_Recursion;

privateArrayList _Files;

#endregion

#region Public Methods

publicvoidBuild()

{

if(_Path!=null){

_Files=newArrayList();

BuildList(_Path,_Recursion);}

else

thrownewException("Path need to be set before you could call this function");

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.

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.

1

2

3

thisStep=(IMashup)Activator.CreateInstanceFrom(

@"assembly.path.dll",@"type.name").Unwrap();

thisStep.ExecuteStep();

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