Saturday, January 30, 2010

StrSpn & StrCSpn in C#

After converting the c/c++ string tokenizing command strtok to c#, I thought I would look around to see there were any other interesting string commands in c/c++ that had not been integrated into c#. What I found was strspn and strcspn.

strspn (String Span) Returns the length of the longest substring that begins at the start of the string and consists only of the characters found in the supplied character array.

Code Snippet

  1. private int strspn(string InputString, char[] Mask)

  2. {

  3.     int count = 0;


  5.     foreach (var c in InputString)

  6.     {

  7.         if (!Mask.Contains(c)) break;


  9.         count++;

  10.     }

  11.     return count;

  12. }

strcspn (String Complement Span) Returns the length of the longest substring that begins at the start of the string and contains none of the characters found in the supplied character array.

Code Snippet

  1.     private int strcspn(string InputString, char[] Mask)

  2.     {

  3.         int count = 0;


  5.         foreach (var c in InputString)

  6.         {

  7.             if (Mask.Contains(c)) break;


  9.             count++;

  10.         }

  11.         return count;

  12.     }

  13. }

Monday, January 25, 2010

C# TechTip #1 Using MaxLength as AutoTab

So, for all those old VB6 programmers out there: Remember The Visual Basic Programmers Journal TechTip publications.

For me, those pamphlets where the most eagerly waited part of my VBPJ publication. Here are the links to PDF versions.

VBPJ TechTips #1
VBPJ TechTips #2
VBPJ TechTips #3
VBPJ TechTips #4
VBPJ TechTips #5
VBPJ TechTips #6
VBPJ TechTips #7
VBPJ TechTips #8
VBPJ TechTips #9
VBPJ TechTips #10
VBPJ TechTips #11
VBPJ TechTips #12

I learned sooooo much from these. The loss of these publications is just one of the reasons that VSM is just a shadow of VBPJ. Just for a walk through memory lane I read through some of them, seeing if any of them would apply to C# today. Most of the tips were not transferable, but a few could be.

So, here is the first "New" C# TechTip: Originally by Karl E. Peterson

How to add an AutoTab feature to textboxes that have a set MaxLength. Connect the textboxes' TextChanged Event to this function:

Code Snippet

  1. private void textBox_TextChanged(object sender, EventArgs e)

  2. {

  3. if((sender as TextBox).MaxLength == (sender as TextBox).TextLength) SendKeys.Send("{TAB}");

  4. }

Who knew that the Dot Net designers had included the SendKeys function!?!

Wednesday, January 20, 2010

Bye bye 1and1 - Hello GoDaddy!

I'm done. I've put up with 1and1's broken ASP.Net implementation. GoDaddy is even cheaper! Well, as I go through this painful experience - I'll keep everyone updated!

Now I can finally do all the things I've learned in ASP.Net 3.5. And, I'll be able to use 4.0 additions before I grow old!

Thank you GoDaddy.

And 1and1? Dudes, it has been five years now and you still have not gotten a full implementation of ASP.Net 2.0 up and running? Wow ...

Friday, January 15, 2010

Dynamically update Url Mappings in Web.Config

One of the most useful yet underused additions to ASP.Net 2.0 was Url Mapping. This feature alone has caused a lot of noise in the blogsphere. Complaints range from the fact that you need an entry for every mapping to the fact that the mappings are not dynamic because they reside in the static web.config file.

Well ... that is all true. It is also true that if you update web.config in code, the application object is restarted and users lose Session state.

But, if Url Mappings is say, used for a standard blog webpage:
  A - How many entries are in a Blog? Even if we are talking a thousand or so entries eventually, will that few lines cause the ASP.Net engine problems? I wouldn't think so.
  B - How often would the webmaster be updating the Url Mappings? Once per posting. Even if the visitors loose Session - its not like it is happening every hour or so.
  C - What is a simple site doing with Session in the first place?

Well, I'm adding Url Mapping to my personal website and I've got a page that when called will automatically update the Url Mappings in web.config from the database, but only when a new mapping is needed.

And yes, I have session and viewstate shut off!

Code Snippet

  1. protected void Page_Load(object sender, EventArgs e)

  2. {

  3. var config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

  4. var section = (UrlMappingsSection)config.GetSection("system.web/urlMappings");

  5. var catArray = new ArrayList();

  6. var arcArray = new ArrayList();

  7. bool AnyChanges = false;

  8. foreach (var bd in BaseData.BlogDataList)

  9. {

  10. foreach (var strCategory in bd.Categories)

  11. {

  12. if (!catArray.Contains(strCategory) && !section.UrlMappings.AllKeys.Contains("~/" + strCategory))

  13. {

  14. catArray.Add(strCategory);

  15. section.UrlMappings.Add(new UrlMapping("~/" + strCategory, "~/Default.aspx?Category=" + strCategory));

  16. AnyChanges = true;

  17. }

  18. }

  19. var strEntryDate = bd.EntryDate.ToString("MMMM yyyy");

  20. if (!arcArray.Contains(strEntryDate) && !section.UrlMappings.AllKeys.Contains("~/" + strEntryDate))

  21. {

  22. arcArray.Add(strEntryDate);

  23. section.UrlMappings.Add(new UrlMapping("~/" + strEntryDate, "~/Default.aspx?Archives=" + strEntryDate));

  24. AnyChanges = true;

  25. }

  26. }

  27. if (AnyChanges) config.Save();

  28. Response.Redirect("~/Default.aspx");

  29. }

  30. }

Sunday, January 10, 2010

Nearly Free Dot Net Books at Amazon

I eagerly await the time of arrival. Like a kid in a candy store, I make plans on what I want.

No, I'm not talking about my Birthday or another Christmas so soon. No, I'm eagerly awaiting Visual Studio 2010 and the update to C# and the Dot Net Framework. But, not for the reasons you might think.

Hard on the heals of a new Visual Studio release is a flood of new Dot Net Books that are updated for the new release. And with that, all the previous release versions are going to be marked down. If history is any indicator, marked almost to the point of being free.

If you do not believe me, search for ... say .... ASP.Net 2.0 (i.e. Visual Studio 2005) and you will find hoards of books for less that a dollar (Plus 3.99 shipping).

Soon after VS2005 arrived I purchased over $1,000 dollars of 1.1 books (Cover Price) for $107 dollars!

But Wait! Why would you want OLD info? Well, new versions add to, but does not alter the Framework, and the framework is so vast that there are always parts that we wanted to learn but didn't want to fork over the $50+ per book to learn.

So, if your like me and your on a budget and want to learn as much as you can, then after March 22nd hit Amazon and load up on VS2008 books!

Tuesday, January 5, 2010

C# Yields a better StrTok Command

Here is another use of the Yield command which allows us to create looping functions that return a value for each iteration. That is actually a great description of the StrTok command in C/C++.

The C/C++ StrTok command takes two parameters, an input string and a string of delimiting characters. After the first call to StrTok, the C/C++ programmer passes a null for the input string and StrTok returns the next Token in the original input string. Sounds like an attempt to create an Enumerable function. Unfortunately standard C/C++ does not have enumerable functions.

Here is a C# enumerable function that you allows you to iterate through the tokens found in the input string. This C# version takes two parameters as well, the input string and an array of delimiting charaters.

Then using built-in power from the .net framework we:

We split the string into a string array using the delimiting characters and we yield return each element in the string array where the element in the string arrays length is greater than 0.

Code Snippet

  1. private IEnumerable<string> StrToken(string TokenizableString, char[] Delimiters)

  2. {

  3.     foreach (var Token in TokenizableString.Split(Delimiters, StringSplitOptions.RemoveEmptyEntries))

  4.     {

  5.         yield return Token;

  6.     }


  8.     yield break;

  9. }