Knowledge.ToString()

.Net Application: How to Use Tokens

Have you come across an application or library which accepts the %token{format} input and based on token and format, the application calculates the value and uses it. You might wonder how easy it is to accept the token and format for your custom application? Here is the ready made code which helps your application define tokens.

I have quickly created a couple of classes which helps your application accept the token and replace it with real value. I have listed following common tokens but you can create your own token based on example.

  • Counter Token – Provide the initial and increment value(negative for decrement) to get the new number each time token is replaced within the string.
  • Current Date Time Token – %date{format} – Gives the current date and time for each token replacement. Each replaced token will get the date and time at that moment
  • Frozen Date Time Token – %frozendate{format} – Gives the date and time when the object was initialized and it will remain the same after each token replacement
  • Text Token – You define your own token and replacement value.

Based on the given tokens, you can easily create tokens for Yesterday, LastWeek, LastMonth, LastYear, NextBusinessDay etc and possibility is endless. That is the beauty of Object Oriented Programming.

Here is an example.

Token Example

Download .Net Application Token Sample Source Code (Visual Studio 2010 Solution)

Share

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *