April 2004 - Posts

System.Int32 vs int

Developers with a C/C++ background are often very aware of using the int data type. The reason for this is that the C/C++ language specification does not dictate a fixed size for the int data type. The advantage of this is that the compiler is free to use the most efficient definition of int for a target environment. So for a 16 bit target, int is defined as being 16 bits while for a 32 bit target int is defined as being 32 bits. This efficiency comes at the cost of portability if the code make assumptions about the definition of the int data type.

The C# language also has an int data type, but unlike C/C++ the int data type is clearly defined as being an alias for the System.Int32 data type. See the Microsoft C# language specification sections 4.1.4 and 4.1.5 and the ECMA C# language specification sections 11.1.3 and 11.1.4. Based on this I believe that for C# we are in no danger of the size of int changing.

I hope that this alleviates some of the concerns that some developers might have and concerning this issue and rest assured that using int is as safe as using System.Int32.

Parsing CSV files into DataTable with RegEx

Just sitting blindly surfing, I came across a routine that appears to do a good job of using RegEx to parse CSV files and build a DataTable. It needs some enhancing to switch between generated columns and using the first row as column headers, also requires some changes to be able to handle very large files, but it seems to be a good starting point. Here is the RegEx used

 ((?<field>[^\",\\r\\n]+)|\"(?<field>([^\"]|\"\")+)\")(,|(?<rowbreak>\\r\\n|\\n|$))

It needs some post processing for the double quoted identifiers, but for the details take a look at the original link

http://www.hotblue.com/article0000.aspx?a=0006

HttpWebRequest and persistent cookies

Today a poster wanted to know how to associate a persistent cookie with its URL when performing a HttpWebRequest to the URL. Fortunately I recently wrote an automated login application in C++ and knew of the WININET function InternetGetCookie, which returns the contents of a cookie related to a URL. I could not find a corresponding .NET method so I put toghether the following piece of code. It lacks decent error handling, but gets the cookies and builds a CookieContainer that can then be assigned to the CookieContainer of a HttpWebRequest.

[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetGetCookie(
  string url, string cookieName,
  StringBuilder cookieData,
ref int size);

private static CookieContainer GetUriCookieContainer(Uri uri)
{
  CookieContainer cookies =
null;
 
 
// Determine the size of the cookie
  int datasize = 256;
  StringBuilder cookieData =
new StringBuilder(datasize);

  if (!InternetGetCookie(uri.ToString(), null, cookieData,
   
ref datasize))
 
{
   
if (datasize < 0) 
     
return null;

    // Allocate stringbuilder large enough to hold the cookie
   
cookieData = new StringBuilder(datasize);
   
if ( !InternetGetCookie(uri.ToString(), null, cookieData,
     
ref datasize) )
     
return null;
 
}

  if (cookieData.Length > 0)
 
{
   
cookies =
new CookieContainer();
   
cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
 
}
 
return cookies;
}